MVC3 - Model empty on post
I have two models -开发者_开发百科 category and article. I have pretty much the same delete views and controllers for both of them. The only difference is that it works for categories, but with articles I get empty model on HttpPost.
Categories:
public ActionResult DeleteCat(int id)
{
Category cat = db.CategoryByID(id);
if (cat != null)
{
return View(cat);
}
return RedirectToAction("Index");
}
[HttpPost]
public ActionResult DeleteCat(Category model)
{
db.DeleteCategory(model.CategoryID);
return RedirectToAction("Index");
}
Articles:
public ActionResult Delete(int id)
{
Article art = db.ArticleByID(id);
if (art != null)
{
return View(art);
}
return RedirectToAction("Index");
}
[HttpPost]
public ActionResult Delete(Article model)
{
db.DeleteArticle(model.ArticleID);
return RedirectToAction("Index");
}
Both views are generated by Visual Studio and I haven't changed them. When I want to delete a category, everything goes well. But when I want to delete an article, it first gets selected properly from database, then the view is displayed (everything is OK) but when I click the delete button the model is empty (all properties are either 0, null or false) and so the db.DeleteArticle throws an exception (there's no article with ArticleID = 0). Could anyone please provide me with any hints as to what should I check or how should I work around this?
If the parameter for the model in the [HttpPost]
Action is the same name as a property in the model it'll be null and will fail validation saying the field was invalid.
Example:
public class ContactMessage
{
public string Name { get; set; }
public string sankdmfskm { get; set; }
}
[HttpPost]
public ActionResult Index(ContactMessage sankdmfskm)
{
...
}
sankdmfskm
will be null
.
Tested in MVC3 and MVC4.
Had same problem. One of my properties in the model was called model
public String model { get; set; }
After renaming the property to myModel. The model object stopped coming back null in ActionResult
Ensure that your model is marking it's properties as properties (if using VB, or C# with get/set), not a public field... MVC won't map to a public field, but will to the public property.
There are two primary ways this can happen.
One way is that you have custom model binding that is not working. I assume you are doing everything out-of-the-box so this wouldn't apply.
The most likely issue is that the data is not getting POSTed. Ensure that the fields exist inside the same Form that the Delete button is POSTing.
I had the same problem as you. I did in your case
[HttpPost]
public ActionResult Index(ContactMessage sankdmfskm)
{
UpdateModel(sankdmfskm);
...
}
After model was updated it returned me proper values and not null anymore.
I'm adding an answer to this because it differs from the others and someone may find it useful.
I had a BaseViewModel as I was using multiple models in one view.
On my Controller Action instead of passing the BaseViewModel (and the parameters like model.RegistrationModel.Username) I passed RegistrationFormModel so it was not binding to the BaseViewModel I was using in my view
see img - the problem I had was that I was passing the RegistrationModel into the Post action instead of the BaseAuthenticationModel (which holds the RegistrationModel) used in the View. So binding wasn't happening correctly
精彩评论