开发者

Html.ValidationMessage renders on all fields, regardless if they're valid or not

开发者 https://www.devze.com 2023-01-31 17:12 出处:网络
My model is correctly validated. If I take a peak in the validation results during debug, I will see that everything is correct. However, all my validation results will show, even if only one is inval

My model is correctly validated. If I take a peak in the validation results during debug, I will see that everything is correct. However, all my validation results will show, even if only one is invalid开发者_如何学运维. Again, during debug, only one field is correctly showing up in the validation results, but when my view is rendered all our displayed:

[HttpPost]
public ActionResult Create(Widget widget)
{
      if (widge.Valid) 
      {
      // Save to db
      }

      retun View(widget);
}

My view:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" Inherits="System.Web.Mvc.ViewPage<Widget>" %>
// took out a lot of html here
<form action="Create" method="post">
<input name="Widget.City" value="<%= Model.City == null ? "" : Model.City%>" />
<%= Html.ValidationMessage("Widget.City")%>
<input name="Widget.Department" value="<%= Model.Department == null ? "" : Model.Department %>" />
<%= Html.ValidationMessage("Widget.Department")%>
<button type="submit">Save</button>
</form>

Let us say City and Department are set to NotNull in my model and I correctly put in a City, but leave Department blank. Again, it will show it is invalid on the controller, with the property Department having a problem, yet in my view I'll get "may not be null" messages for both properties. If I have 4 properties and 4 ValidationMessage tags in my view, even if one property is not valid ... all 4 will show. What's the deal?


If I'm not mistaken, I think you want to be using

Html.ValidationMessageFor(model => model.City)

Not what you're using currently in your view.

Also... Since you're using a strongly typed view, you should be checking ModelState.IsValid to determine whether or not you should save your Widget. That is if you're using data annotations on your view model.


What are you using as your input to the [HttpGet] action of the view?

Since you're using the format Widget.Property the view expects the model to have a Widget property containing the data for the widget.. i.e it is looking for Model.Widget.Property but your model only contains Model.Property. Based on the code posted here, you're only passing the widget back to the view and it will interpret that as having a NULL Widget property, thus triggering all NotNull validation.

A solution here is to assign the Widget post model (what you're accepting in the [HttpPost] action) to a Widget property of the of the model you're passing back to the view.

public class CreateViewModel
{
    public Widget Widget { get; set; }
}


[HttpPost]
public ActionResult Create(Widget widget)
{
      if (widge.Valid) 
      {
      // Save to db
      }
      var viewModel = new CreateModel() { Widget = widget };

      retun View( viewModel );
}

Hopefully I articulated that correctly.. Its been a long day = )

0

精彩评论

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