Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom shape model EDIT problem
    text
    copied!<p>I have a person table which has two properties: ID, nameID; And a personName table which has four properties: NameID, Firstname, Middlename, Lastname; Here nameID is a foreign key form PersonName table to PersonTable; (Hope you understand i am here to test the multi valued attribute test.)</p> <p>Then i made a ADO.net datamodel of these two tables. After that i made a custom shape model called NameViewData which looks like:</p> <pre><code>public class NameViewData { public Person Person { get; set; } public PersonName Name { get; set; } public NameViewData(Person person, PersonName personName) { Person = person; Name = personName; } } </code></pre> <p>I created a name controller where i wish to use CRUD operations. In this controller i wrote the Edit action(GET Method) like this:</p> <pre><code>public ActionResult Edit(int id) { Person person = db.Person.First(x=&gt;x.Id == id); PersonName personName = db.PersonName.First(x=&gt;x.NameId == id); return View(new NameViewData(person, personName)); } </code></pre> <p>Now the problem is that i am not sure how to write the Edit action (POST method) and the Edit View page. I wrote like this:</p> <p>Edit View:</p> <pre><code>Inherits="System.Web.Mvc.ViewPage&lt;Name.Models.NameViewData&gt; &lt;% using (Html.BeginForm()) {%&gt; &lt;fieldset&gt; &lt;legend&gt;Fields&lt;/legend&gt; &lt;p&gt; &lt;label for="Name.Firstname"&gt;Firstname:&lt;/label&gt; &lt;%= Html.TextBox("Name.Firstname", Model.Name.Firstname)%&gt; &lt;%= Html.ValidationMessage("Name.Firstname", "*")%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="Name.Middlename"&gt;Middlename:&lt;/label&gt; &lt;%= Html.TextBox("Name.Middlename", Model.Name.Middlename)%&gt; &lt;%= Html.ValidationMessage("Name.Middlename", "*")%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="Name.Lastname"&gt;Lastname:&lt;/label&gt; &lt;%= Html.TextBox("Name.Lastname", Model.Name.Lastname)%&gt; &lt;%= Html.ValidationMessage("Name.Lastname", "*")%&gt; &lt;/p&gt; &lt;p&gt; &lt;input type="submit" value="Save" /&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;% } %&gt; </code></pre> <p>Edit POST:</p> <pre><code>[AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(int id, FormCollection collection) { Person person = db.Person.First(x=&gt;x.Id == id); PersonName personName = db.PersonName.First(x=&gt;x.NameId == id); NameViewData nameViewData = new NameViewData(person, personName); try { TryUpdateModel(nameViewData); if (ModelState.IsValid) { db.AddToPersonName(nameViewData.Name); db.AddToPerson(nameViewData.Person); db.SaveChanges(); } return RedirectToAction("Index"); } catch { return View(); } } </code></pre> <p>But this not working. Can you please help me out. And also what should i read about custom shape view model, provide any link if possible.</p>
 

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