Note that there are some explanatory texts on larger screens.

plurals
  1. POEnough MVC to be dangerous
    primarykey
    data
    text
    <p>I have built and EF5 model of my DB and have it in a separate project that I reference in my MVC project. I have several controllers and associated views to allow the data in the model to be rendered view razor to the UI. When it comes to editing a record, there are some foreign key relationships as show in the attached model diagram. <img src="https://i.stack.imgur.com/OrjhG.png" alt="enter image description here"></p> <p>I can see the data in the index view and am able to navigate to the create view as shown below:</p> <p><img src="https://i.stack.imgur.com/1SLly.png" alt="enter image description here"></p> <p><img src="https://i.stack.imgur.com/fJayZ.png" alt="enter image description here"></p> <p>However, the drop down that contains the list of vendors only shows the first property from the vendor object. This is not what I want the view to do.</p> <p>My GET and POST code look like this:</p> <pre><code> // // GET: /Property/Create public ActionResult Create() { ViewBag.PropertyType = new SelectList(db.PropertyTypes, "Id", "PropertyTypeName"); ViewBag.VendorID = new SelectList(db.Vendors, "ID", "Title"); return View(); } // // POST: /Property/Create [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(Property property) { if (ModelState.IsValid) { db.Properties.Add(property); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.PropertyType = new SelectList(db.PropertyTypes, "Id", "PropertyTypeName", property.PropertyType); ViewBag.VendorID = new SelectList(db.Vendors, "ID", "Title", property.VendorID); return View(property); } </code></pre> <p>and my view razor code looks like this:</p> <pre><code>@model Model.Property @{ ViewBag.Title = "Create"; } &lt;h2&gt;Create&lt;/h2&gt; @using (Html.BeginForm()) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;Property&lt;/legend&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.PropertyName) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.PropertyName) @Html.ValidationMessageFor(model =&gt; model.PropertyName) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.PropertyNumber) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.PropertyNumber) @Html.ValidationMessageFor(model =&gt; model.PropertyNumber) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.PropertyStreet) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.PropertyStreet) @Html.ValidationMessageFor(model =&gt; model.PropertyStreet) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.PropertyTown) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.PropertyTown) @Html.ValidationMessageFor(model =&gt; model.PropertyTown) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.PropertyPostcode) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.PropertyPostcode) @Html.ValidationMessageFor(model =&gt; model.PropertyPostcode) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.PropertyType, "PropertyType1") &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DropDownList("PropertyType", String.Empty) @Html.ValidationMessageFor(model =&gt; model.PropertyType) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.VendorID, "Vendor") &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DropDownList("VendorID", string.Empty) @Html.ValidationMessageFor(model =&gt; model.VendorID) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;/fieldset&gt; } &lt;div&gt; @Html.ActionLink("Back to List", "Index") &lt;/div&gt; @section Scripts { @Scripts.Render("~/bundles/jqueryval") } </code></pre> <p>Can anyone help me with the solution to change the property being shown in the dropdown to a different one (I have created a calculated field in the model as I thought that might be easier to display as the value for the dropdown) or a concatenation of the other name fields shown in the model in the create action of the controller so the view move information in the dropdown list?</p> <p>Many thanks,</p> <p>Jason.</p> <p>EDIT</p> <p>I have now changed my create action to the following as suggested:</p> <pre><code>public ActionResult Create() { ViewBag.PropertyType = new SelectList(db.PropertyTypes, "Id", "PropertyTypeName"); ViewBag.VendorID = new SelectList(db.Vendors.Select(v =&gt; new SelectListItem { Value = v.ID, Text = string.Format("{0} {1} {2}", v.Title, v.Firstname, v.Secondname) }), "Text", "Value"); return View(); } </code></pre> <p>But now it won't compile. The m.id property I'm trying to assign to Value is an int (it's the primary key of the vendors table) but I can't cast it to a string otherwise the Razor engine raises an error when the view is rendered.</p> <p>Any more ideas or help about how to overcome this apparently simple task.</p> <p>Thanks again.</p> <p>J.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload