开发者

Edit Operation Validation for ASP.NET MVC

The following codes works. But the problem is when I click the edit link to edit a product the all the text boxes gets cleared but error message are working. This is happening because Strongly Typed View class is selected which contains error message with regex. That class is being used for Create operation as well. The thing is that I want to include the errors message if a text(es) is remained empty or invalid data is enter and user tries to save in those conditions. How do can this be achieved, if possible. I am using VS 2010, MVC C#, ADO.NET.

[HttpPost]
public ActionResult Edit(int id, FormCollection collection, 
                         ProductCategory editCategory)
{
    if (!ModelState.IsValid)
    {
            return View(editCategory);
    }

    var categoryToUpdate = db.ProductCategories.First(m => 
    m.ProductCategoryID == id);
    ViewData.Model = categoryToUpdate;
    TryUpdateModel(categoryToUpdate, ne开发者_Go百科w string[] { }, 
    collection.ToValueProvider());
    db.AddToProductCategories(editCategory);
    db.SaveChanges(); 
    return View("/");
}


You need to send your model back.

If your view is named Edit you just need to:

return View(editCategory);


I figured it out. I coded error syntax in edit operation and it is working.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜