NOTE: Code updated to take into account the comments and answer made below.
In my MVC app I need sometimes to make references to other objects (like a many-to-many relationship, or one-to-many relationship).
So I have this model:
public class ObjInfo
{
[Required(ErrorMessage = "Obj ID is required.")]
[Display(Name = "ObjID")]
public int m_Id { get; set; }
[Required(ErrorMessage = "Obj Name is required.")]
[Display(Name = "Obj Name")]
public string m_Name { get; set; }
[Display(Name = "Obj Number")]
public int m_Number { get; set; }
(...)
[Required(ErrorMessage = "Other Obj is required.")]
[Display(Name = "OtherObj")]
public int m_OtherObjID { get; set; }
public OtherObjInfo m_OtherObj { get; set; }
(...)
}
I have default and parameters constructors as well and can show them as needed, though I am not sure if they are at fault. Anyway.
In my controller, I have the two create methods following MVC methods:
//
// GET: /Obj/Create
public ActionResult Create()
{
ViewBag.List = new SelectList(PopulateDDLs(), "OtherObj", "Other obj ID");
return View();
}
//
// POST: /Obj/Create
[HttpPost]
public ActionResult Create(Objinfo obj)
{
if (ModelState.IsValid)
{
m_ObjManager.CreateObj(obj);
return RedirectToAction("SearchIndex");
}
ViewBag.List = new SelectList(PopulateDDLs(), "OtherObj", "Other obj ID");
return View(obj);
}
And, finally, here’s how my “Create” view is coded:
@model MyApp.Models.ObjInfo
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>OBJ</legend>
<div class="editor-label">
@Html.LabelFor(model => model.m_Id)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.m_Id)
@Html.ValidationMessageFor(model => model.m_Id)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.m_Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.m_Name)
@Html.ValidationMessageFor(model => model.m_Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.m_Number)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.m_Number)
@Html.ValidationMessageFor(model => model.m_Number)
</div>
(...)
<div class="editor-label">
@Html.LabelFor(model => model.m_OtherObj , "Other Obj")
</div>
<div class="editor-field">
@Html.DropDownList(model => model.m_OtherObjID, ViewBag.List as SelectList, "--- Select Other Obj ---", new {@class = "OtherObjInfo "})
@Html.ValidationMessageFor(model => model.m_OtherObj)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Basically, the problem is that each time I click on the “create” button, the ModelState validation is always false for the OtherObj even if something is selected in the dropdownlist. Every other values are correct except this one.
I don’t understand why and I would greatly appreciate your help on this one, thank you for your help!
After code edited **
Now I get a crash as I enter the “Create” view:
DataBinding: 'System.String' does not contain a property with the name 'OtherObj'.
Exactly on the line where my dropdownlistfor is located.
The datavalueField and dataTextField are supposed to refer to what exactly?
add
otherObjectIdto your modelcontroller
view
better way is using strongly typed helpers. All your helpers are
strongly-typed(editorfor, textboxfor, dropdownlistfor,…) except dropdownlist.if you want to bind DDL value to your model, You should use
dropdownlistforinstead ofdropdownlist.your model state is not valid, because you dont bind required value as DDL to model.