29.05.2015 Views

o_19mgorv9t13a3ko71fev19l81mqa.pdf

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Listing 11-17. Explicitly Specifying an Action Method and Controller for a Form in the Edit.cshtml File<br />

@model SportsStore.Domain.Entities.Product<br />

@{<br />

}<br />

ViewBag.Title = "Admin: Edit " + @Model.Name;<br />

Layout = "∼/Views/Shared/_AdminLayout.cshtml";<br />

<br />

<br />

Edit @Model.Name<br />

<br />

@using (Html.BeginForm("Edit", "Admin")) {<br />

<br />

@foreach (var property in ViewData.ModelMetadata.Properties) {<br />

if (property.PropertyName != "ProductID") {<br />

<br />

@(property.DisplayName ??<br />

property.PropertyName)<br />

@if (property.PropertyName == "Description") {<br />

@Html.TextArea(property.PropertyName, null,<br />

new { @class = "form-control", rows = 5 })<br />

} else {<br />

@Html.TextBox(property.PropertyName, null,<br />

new { @class = "form-control" })<br />

}<br />

@Html.ValidationMessage(property.PropertyName)<br />

<br />

}<br />

}<br />

<br />

new {<br />

}<br />

<br />

<br />

<br />

@Html.ActionLink("Cancel and return to List", "Index", null,<br />

})<br />

<br />

@class = "btn btn-default"<br />

Now the form will always be posted to the Edit action, regardless of which action rendered it. I can now create products by<br />

clicking the Add a new product link and filling in the details, as shown in Figure 11-10.<br />

296

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!