Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC 4 Error Saving ViewModels
    primarykey
    data
    text
    <p>Can somebody help me on how to save and update data into multiple entities using a ViewModel?</p> <p>I have a ViewModel that looks like this:</p> <pre><code> public class StudentViewModel { public Student student; public StudentAddress studentAddress { get; set; } public StudentPhoto studentPhoto { get; set; } // Three entities are related to one to one relationship public StudentViewModel() { } } </code></pre> <p>My Controller is:</p> <pre><code> [HttpPost] public ActionResult Create(StudentViewModel studentViewModel) { if (ModelState.IsValid) { return View(studentViewModel); } Student s = new Student() { Name =studentViewModel.Student.Name, Speciality = studentViewModel.Student.Speciality, DateOfReg = studentViewModel.Student.DateOfJoinig, Qualification = studentViewModel.Student.Qualification, Email = studentViewModel.Student.Email }; StudentAddress sa = new StudentAddress() { StudentId= studentViewModel.Student.StudentId, Address = studentViewModel.StudentAddress.Address, Area = studentViewModell.StudentAddress.Area, City = studentViewModel.StudentAddress.City, State = studentViewModel.StudentAddress.State, Mobile = studentViewModel.StudentAddress.Mobile }; StudentPhoto sp = new StudentPhoto() { StudentId= studentViewModel.Student.StudentId, Photo = studentViewModel.StudentPhoto.Photo }; db.Students.Add(s); db.StudentAddress.Add(sa); db.StudentPhoto.Add(sp); db.SaveChanges(); return RedirectToAction("Home"); } </code></pre> <p>View is: </p> <pre><code>@using (Html.BeginForm()) { @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;Doctor&lt;/legend&gt; @Html.EditorFor(model =&gt; model.Student.Name ) @Html.EditorFor(model =&gt; model.Student.Speciality) @Html.EditorFor(model =&gt; model.Student.DateOfJoinig) @Html.EditorFor(model =&gt; model.Student.Standard) @Html.HiddenFor(model =&gt; model.Student.StudentId) @Html.EditorFor(model =&gt; model.StudentAddress.Address) @Html.EditorFor(model =&gt; model.StudentAddress.Area) @Html.EditorFor(model =&gt; model.StudentAddress.City) @Html.EditorFor(model =&gt; model.StudentAddress.State) @Html.HiddenFor(model =&gt; model.Student.StudentId) @Html.EditorFor(model =&gt; model.StudentPhoto.Photo) &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; </code></pre> <p>I was able to retrieve and display the data (from multiple entities) into the view. However, now I'm stuck on how can I save and update the above entities with the new data. Most of the examples are 1-1 relationship the mapping is automatic, but in this case the data belongs to multiple entities.</p> <p>My problem is when i try to save data it redirected to the create page. "ModelState.IsValid" is false always so no data saved. Please help me how do i proceed. </p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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