简体   繁体   English

添加ModelState.AddModelError而无需重新加载页面(mvc4)

[英]Add ModelState.AddModelError without reload the page (mvc4)

I'm using MVC4 and I have 2 methods in my controller: 我正在使用MVC4,控制器中有2种方法:

Get Method 获取方法

public ActionResult Create()
{
    var vm = new User
    {
        Client= new Catastro_Cliente()
        Genre = ClienteRepository.GetGenres(),
        Type= ClienteRepository.GetTypes()
    };

    ViewBag.Genre = new SelectList(vm.Genre, "IdGenre", "Genre");
    ViewBag.Type= new SelectList(vm.Type, "IdType", "Type");
    return View(vm);
}

Post Method 过帐方法

[HttpPost]
public ActionResult CrearUsuario(Catastro_Cliente client)
{
    if(Validator(client.document))
    {
        ModelState.AddModelError("NoDocument", "The document is invalid or not registered");
    }

    if(ModelState.IsValid)
    {
        return View();
    }
}

Basically, I'm trying keep all the data that the user filled in before the post Method, 基本上,我正在尝试保留用户在post Method之前填写的所有数据,

I tried with return RedirectToAction("Create"); 我尝试使用return RedirectToAction("Create"); but it always refreshes the page. 但它总是刷新页面。

You have to pass back the posted model when you call View . 调用View时,您必须传递回已发布的模型。 What you need is something like: 您需要的是这样的:

[HttpPost]
public ActionResult CrearUsuario(Catastro_Cliente client)
{
    if(Validator(client.document))
    {
        ModelState.AddModelError("NoDocument", "The document is invalid or not registered");
    }

    if(ModelState.IsValid)
    {
        // save to database of whatever
        // this is a successful response
        return RedirectToAction("Index");
    }

    // There's some error so return view with posted data:
    return View(client);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当MVC4中的模型为空时如何显示ModelState.AddModelError - How to show ModelState.AddModelError when the Model is Empty in MVC4 ASP.NET MVC 2 RC在表单级别上的ModelState.AddModelError - ModelState.AddModelError on form level with ASP.NET MVC 2 RC 未在PartialView中显示Modelstate.addmodelerror - Modelstate.addmodelerror not showing in PartialView 创建一个类型化的ModelState.AddModelError() - Creating a typed ModelState.AddModelError() ModelState.AddModelError 但没有 ErrorMessage(甚至是默认值) - 仅突出显示该字段 - ModelState.AddModelError but without the ErrorMessage (even default) - only highlight the field 通过javascript提交表单后将ModelState.AddModelError添加到视图中 - Add ModelState.AddModelError to the view after submitting form through javascript 未绑定模型项时如何添加 ModelState.AddModelError 消息 - How to add ModelState.AddModelError message when model item is not binded 在不检查ModelState.IsValid的情况下将ModelState.AddModelError添加到我的Controller类不会有任何效果 - Adding ModelState.AddModelError to my Controller class, without checking for ModelState.IsValid will not have any effect 更改 ModelState.AddModelError 消息的字体颜色 - Changing the FONT color of the ModelState.AddModelError message ASP.NET MVC中ModelState.AddModelError中的关键参数有什么意义? - What is the point of the key parameter in ModelState.AddModelError in ASP.NET MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM