Note that there are some explanatory texts on larger screens.

plurals
  1. POViewModel update - changing properties before saving
    primarykey
    data
    text
    <p>After trying around the whole day with model bindings, without results, i decided to ask here.</p> <p>I have got an asp.net razor view where a user (aka Seller) can edit his user details. Furthermore the user should be able to change his password.</p> <p>I made a ViewModel:</p> <pre><code>public class EditSellerViewModel { public Seller Seller { get; set; } public ChangePasswordModel ChangePasswordModel { get; set; } } </code></pre> <p>My view has two forms which result in two "Submit" buttons. In my action i check which button was clicked. If the "Passwords" form has been submitted, i want to set the new Password in the Seller entity (that actually works) and SaveChanges() which does not change anything in the database (and does not throw any exception). It simply does nothing.</p> <p>Furthermore if the "Seller Detail" form was submitted, i want to save the sellers data. But TryUpdateModel is always false, even if i use the second parameter which enables the prefix for ViewModels.</p> <pre><code> [HttpPost] public ActionResult EditUser(string btnSubmit, FormCollection formValues, EditSellerViewModel editSellerViewModel) { int uid = baseFunc.GetIdForUsername(User.Identity.Name); var seller = bmDBCont.SellerSet.Single(s =&gt; s.Id == uid); if (btnSubmit == "saveSellerPassword") { seller.Password = editSellerViewModel.ChangePasswordModel.NewPassword; bmDBCont.ObjectStateManager.ChangeObjectState(seller, System.Data.EntityState.Modified); bmDBCont.SaveChanges(); //&lt;-- does nothing } if (TryUpdateModel(seller, "Seller")) //&lt;-- never true { bmDBCont.SaveChanges(); return RedirectToAction("Index"); } ViewBag.Titles = CommonListsProvider.GetTitles(); ViewBag.Countries = CommonListsProvider.GetCountries(); return View(editSellerViewModel); } </code></pre> <p>Here some debug info screenshots:</p> <ul> <li><a href="http://i.stack.imgur.com/oKINW.jpg" rel="nofollow">formcollection with seller form submitted</a></li> <li><a href="http://i.stack.imgur.com/W59AR.jpg" rel="nofollow">formcollection with password form submitted</a></li> </ul> <p>Please can anyone help me?</p>
    singulars
    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.
 

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