简体   繁体   中英

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:

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?

Nice question, the Bind attribute will improve the performance by only bind properties which you needed.

You can check if this will cause any problem by using the ModelState entity.

Inside your controller, the first thing you do is checking the ModelState use the following instruction:

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

If you're ModelState is valid. You can consider that there are no problems, and proceed with whatever you want to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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