开发者

Asp.Net Mvc - Don't clear error input in form

When I validate a form with Asp.Net Mvc and there's an error on the form, Asp.Net return me the form with the correct error message but clear the content of the input. I would like the content of the input to stay even if this content is wrong. How can I do that ?

UPDATE

Maybe this is because I don't use the default validation开发者_运维技巧. I use the Fluent Validation library.

Here what I do :

var validator = new UserValidator();
var results = validator.Validate(user);
results.AddToModelState(ModelState, "");

if (!ModelState.IsValid) 
{
     return View("SignUp", user);
}


The problem might be in how you "bind" the model you are passing in the view. If you use a strongly typed view and create the input fields with for example

<%=Html.TexboxFor(m=>m.UserName)%> 

or

<%=Html.TextBox("UserName", Model.UserName)%>

then you should see the values after posting. Regards


In addition to what @Germán Ubillos posted, you can also store the post values in TempData and send them back through.

<%=Html.TextBox("UserName", TempData["UserName"])%>

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SomeAction(string UserName)
    {
        TempData["UserName"] = UserName;

        // Do your validation
        var validator = new UserValidator();
        var results = validator.Validate(user);
        results.AddToModelState(ModelState, "");

        if (!ModelState.IsValid) 
        {
            return View("SignUp", user);
        }
        //return some view
    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜