TryUpdateModel 在属性值更改后不更新对象

发布于 2024-09-24 03:23:40 字数 1374 浏览 2 评论 0原文

我正在使用 MVC 2 和 EF4。我有一个显示我的应用程序(类)属性的视图。并非所有属性都显示在视图中。单击提交按钮后,需要设置几个属性。

我正在通过客户端验证,但我的服务器验证仍然失败。我在 CreateApplication 操作中收到一个 Application 对象,更新属性,并执行 ModelState.IsValid 检查。这仍然是错误的。我循环浏览了错误列表,它显示了我使用必需数据注释在 SubmitterEmployeeNumber 属性上设置的错误文本。我确实设置了它并且更新了我的模型,但验证仍然失败。这是我的代码:

[HttpPost]
public ActionResult CreateApplication(Application application)
{
   application.SubmitterEmployeeNumber = "123456";

   TryUpdateModel(application);

   if (ModelState.IsValid)
   {
   }
}

这是我显示视图的方式:

public ActionResult CreateApplication()
{
   var viewModel = new ApplicationViewModel(new Application(), db.AccountTypes);

   return View(viewModel);
}

绑定后设置属性后如何让验证通过?

UpdateModelTryUpdateModel 之间有什么区别?何时需要使用它们?

编辑:

我将操作的名称更改为:

[HttpPost]
public ActionResult CreateApp()
{
   var application = new Application
   {
      ApplicationStateID = 1,
      SubmitterEmployeeNumber = "123456"
   };

   if (TryUpdateModel(application))
   {
      int success = 0;
   }
}

这是我的观点:

<% using (Html.BeginForm("CreateApp", "Application")) {%>

TryUpdateModel 仍然验证为 false。我输入 int success = 0; 只是为了看看它是否会进入其中,但它不会。

I'm using MVC 2 and EF4. I have a view that displays my Application (class) properties. Not all the properties are displayed in the view. There are a couple of the properties that need to be set once the submit button is clicked.

I'm getting client validation to pass, but my server validation is still failing. I receive an Application object in my CreateApplication action, I update a property, and do a ModelState.IsValid check. It is still false. I did a loop through my errors list and it displays the error text that I set on my SubmitterEmployeeNumber property using a Required data annotation. I did set it and I did update my model, but validation is still failing. Here is my code:

[HttpPost]
public ActionResult CreateApplication(Application application)
{
   application.SubmitterEmployeeNumber = "123456";

   TryUpdateModel(application);

   if (ModelState.IsValid)
   {
   }
}

Here is how I display the view:

public ActionResult CreateApplication()
{
   var viewModel = new ApplicationViewModel(new Application(), db.AccountTypes);

   return View(viewModel);
}

How do I get the validation to pass after I set the property after binding?

What is the difference between UpdateModel and TryUpdateModel and when do I need to use each?

EDIT:

I changed the name of the action to:

[HttpPost]
public ActionResult CreateApp()
{
   var application = new Application
   {
      ApplicationStateID = 1,
      SubmitterEmployeeNumber = "123456"
   };

   if (TryUpdateModel(application))
   {
      int success = 0;
   }
}

Here is my view:

<% using (Html.BeginForm("CreateApp", "Application")) {%>

TryUpdateModel still validates as false. I put in int success = 0; just to see if it will go into it but it doesn't.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过潦 2024-10-01 03:23:40
[HttpPost]
public ActionResult CreateApplication()
{
    var application = new Application 
    {
        SubmitterEmployeeNumber = "123456"
    };
    if (TryUpdateModel(application)) 
    {
        // The model is valid => submit values to the database
        return RedirectToAction("Success");
    }
    return View(application);
}

更新:由于评论部分存在许多混乱,这里有一个完整的工作示例。

模型:

public class Application
{
    [Required]
    public int? ApplicationStateID { get; set; }

    [Required]
    public string SubmitterEmployeeNumber { get; set; }

    [Required]
    public string Foo { get; set; }

    [Required]
    public string Bar { get; set; }
}

控制器:

[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        var application = new Application();
        return View(application);
    }

    [HttpPost]
    [ActionName("Index")]
    public ActionResult Create()
    {
        var application = new Application
        {
            ApplicationStateID = 1,
            SubmitterEmployeeNumber = "123456"
        };
        if (TryUpdateModel(application))
        {
            // success => update database, etc...
            return Content("yupee");
        }

        // failure => redisplay view to fix errors
        return View(application);
    }
}

视图:

<% using (Html.BeginForm()) { %>
    <div>
        <%: Html.LabelFor(x => x.Foo) %>
        <%: Html.TextBoxFor(x => x.Foo) %>
        <%: Html.ValidationMessageFor(x => x.Foo) %>
    </div>

    <div>
        <%: Html.LabelFor(x => x.Bar) %>
        <%: Html.TextBoxFor(x => x.Bar) %>
        <%: Html.ValidationMessageFor(x => x.Bar) %>
    </div>

    <input type="submit" value="GO GO" />
<% } %>

希望这能解决问题。

[HttpPost]
public ActionResult CreateApplication()
{
    var application = new Application 
    {
        SubmitterEmployeeNumber = "123456"
    };
    if (TryUpdateModel(application)) 
    {
        // The model is valid => submit values to the database
        return RedirectToAction("Success");
    }
    return View(application);
}

UPDATE: Due to many confusions in the comments section here's a full working example.

Model:

public class Application
{
    [Required]
    public int? ApplicationStateID { get; set; }

    [Required]
    public string SubmitterEmployeeNumber { get; set; }

    [Required]
    public string Foo { get; set; }

    [Required]
    public string Bar { get; set; }
}

Controller:

[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        var application = new Application();
        return View(application);
    }

    [HttpPost]
    [ActionName("Index")]
    public ActionResult Create()
    {
        var application = new Application
        {
            ApplicationStateID = 1,
            SubmitterEmployeeNumber = "123456"
        };
        if (TryUpdateModel(application))
        {
            // success => update database, etc...
            return Content("yupee");
        }

        // failure => redisplay view to fix errors
        return View(application);
    }
}

View:

<% using (Html.BeginForm()) { %>
    <div>
        <%: Html.LabelFor(x => x.Foo) %>
        <%: Html.TextBoxFor(x => x.Foo) %>
        <%: Html.ValidationMessageFor(x => x.Foo) %>
    </div>

    <div>
        <%: Html.LabelFor(x => x.Bar) %>
        <%: Html.TextBoxFor(x => x.Bar) %>
        <%: Html.ValidationMessageFor(x => x.Bar) %>
    </div>

    <input type="submit" value="GO GO" />
<% } %>

Hope this clears things up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文