I have a very simple MVC5 application that has a product page for a client that I also use the basic CRUD operations that were built in MVC 5.
I have a model called Cakes.cs because the client sells cakes. Pretty simple. Here is the code for this model:
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace TastyCakes.Models { public class Cakes { public int CakesID { get; set; } public string Name { get; set; } public string Description { get; set; } public decimal Price { get; set; } public string CakeImage { get { return Name.Replace(" ", string.Empty) + ".jpg"; } } } }
As you can see, I use the computed property to create an image name for each cake. I need only 1 image for each cake. Now that I go to edit the cake on my CRUD pages. I would like to add a simple image upload that will upload the image (no resizing or thumbnails needed). But I would like to overlay the computed property name. In other words: no matter what user named his photo, my upload code will rename it to everything that has Cakes.Name (minus any spaces) + ". Jpg", and save it in "~ Images / Cakes".
All I need is for the download to be on the real edit page, so the cupcake has already been created at this point. All information necessary for renaming a file should be accessible and convenient for use on the editing page. Below is my page editing code:
Change page:
@model TastyCakes.Models.Cakes <div class="row"> <div class="large-12 columns"> <hgroup class="title"> <h1>Edit Cakes</h1> </hgroup> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <hr /> @Html.ValidationSummary(true) @Html.HiddenFor(model => model.CakesID) <div class="medium-12 column"> @Html.LabelFor(model => model.Name) @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) </div> <div class="medium-12 column"> @Html.LabelFor(model => model.Description) @Html.EditorFor(model => model.Description) @Html.ValidationMessageFor(model => model.Description) </div> <div class="medium-12 column"> @Html.LabelFor(model => model.Price) @Html.EditorFor(model => model.Price) @Html.ValidationMessageFor(model => model.Price) </div> <div class="medium-12 column"> <input type="submit" value="Save" class="tiny button" /> @Html.ActionLink("Back to List", "Index", null, new { @class = "tiny button" }) </div> </div> } @section Scripts { @Scripts.Render("~/bundles/jqueryval") } </div> </div>
I looked at several html5 and ASP.Net 4 solutions, but that is too much. I want it very simple. Any ideas or a hit in the right direction would be greatly appreciated. I use this code not only for the customers website, but also for posting on a fictitious website used to teach very simple MVC concepts.
source share