开发者

Validation on DropDownListFor problem (ModelState.IsValid always false)

I´m having problem using validation on DropDownListFor ...

My Model :

public class User 
{
    ...
    public virtual int Id{ get; set; }
    [Required(ErrorMessage = "Required.")]
    public virtual Role Role { get; set; }
}

public class Role 
{        
    public virtual int Id{ get; set; }
    [Required(ErrorMessage = "Required.")]
    public virtual string Name { get; set; }
}

My Controller:

    public ActionResult Edit()
    {
        ViewBag.Roles = new SelectList(new R开发者_如何学JAVAepository<Role>().GetAll(), "Id", "Name");

        return View();
    }

My Edit View(Strongly typed)

   @model User
   ...
   @Html.LabelFor(model => model.Role)
   @Html.DropDownListFor(model => model.Role.Id, ViewBag.Roles as SelectList, "-- Select --",new { @class = "form radius" })
   @Html.ValidationMessageFor(model => model.Role)

All works great, the problem is fired when I tried to Save the User model :

    [HttpPost]
    public ActionResult Edit(User user)
    {
        if (ModelState.IsValid)
        ...
    }

The ModelState.IsValid is always false... Looking inside the ModelState, the the error is in Role.Name key... The error is : The Name field is required

Is there any way to fix that ? How?

Thanks


You have placed a Required attributes on the Name property but this is never sent to the server so your model is not valid (only the Id is POSTed). So you should place it on the Id property:

public class Role 
{        
    [Required(ErrorMessage = "Required.")]
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}

Also you should make this Id property Nullable as you have defined an empty value for your dropdown and if the user doesn't select any value it will crash. So your model should look like this:

public class Role 
{        
    [Required(ErrorMessage = "Required.")]
    public virtual int? Id { get; set; }
    public virtual string Name { get; set; }
}

Also you could remove the Required attribute from the Role property in the User class. It's not needed.

Final advice and probably the most important one: replace this ViewBag ugliness with a property on your view model so that the helper looks like this:

@Html.DropDownListFor(
    model => model.Role.Id, 
    Model.Roles, 
    "-- Select --",
    new { @class = "form radius" }
)


I also encountered this kind of error.

Maybe the solution to your problem is placing a code like this in your Edit.cshtml view:

@Html.HiddenFor(model => model.Id)


Model

public class Role 
{        
    [Required(ErrorMessage = "Id Required.")]
    public virtual int Id { get; set; }
    [Required(ErrorMessage = "Name Required.")]
    public virtual string Name { get; set; }
}

View

@Html.DropDownListFor(m => m.Role.Id, (SelectList)ViewBag.gRoles, "-- Select --")
@Html.ValidationMessageFor(m => m.Role.Id)

Controller

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Creedit(User x)
{
    x.Role = db.RoseSet.Find(x.Role.Id);
    if (x.Role != null)
    {
        ModelState["Role.Name"].Errors.Clear();

    }

    if (ModelState.IsValid)
    {
      // proceed
    }
    else
    {
      // return validation error
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜