简体   繁体   English

Required属性是否适用于属性在传递给Asp.Net mvc中的Actionresult时从Model中的绑定中排除

[英]Does Required attribute works for properties Excluded from binding in Model while passing to Actionresult in Asp.Net mvc

This is my model: 这是我的模特:

public class JQueryDataTableParamModel
{
     /// <summary>
     /// Request sequence number sent by DataTable, same value must be returned in response
     /// </summary>   
     [Required]
     public string sEcho { get; set; }

     /// <summary>
     /// Text used for filtering
     /// </summary>
     [Required]
     public string sSearch { get; set; }
}

This is my ActionResult: 这是我的ActionResult:

public ActionResult VolumeOverviewHandler([Bind(Include = "sEcho")]JQueryDataTableParamModel param)

My question is does the Required attribute on 2nd property going to create issues in this case? 我的问题是第二个属性的Required属性是否会在这种情况下产生问题?

Nice question, the Bind attribute will improve the performance by only bind properties which you needed. 不错的问题, Bind属性将仅通过您需要的绑定属性来提高性能。

You can check if this will cause any problem by using the ModelState entity. 您可以使用ModelState实体检查这是否会导致任何问题。

Inside your controller, the first thing you do is checking the ModelState use the following instruction: 在控制器内部,您要做的第一件事是检查ModelState使用以下指令:

if(!ModelState.IsValid){ throw new someException(); or return BadRequest("Model Is Not Valid");}

If you're ModelState is valid. 如果您的ModelState有效。 You can consider that there are no problems, and proceed with whatever you want to do. 您可以认为没有问题,并继续您想做的任何事情。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM