Parameter Name in Remote Model Validation Action o

2019-03-21 15:16发布

I use Remote validation attribute for SSN property, In view Page I use generic view then the ssn field is like:

@Html.EditorFor(model => model.MainModel.SSN)
@Html.ValidationMessageFor(model => model.MainModel.SSN)

and My Action is:

public JsonResult IsValidaSSN(string SSN) {

  //....

    return Json(result, JsonRequestBehavior.AllowGet);

}

but always SSN is null in action, I also try MainModelSSN, MainModel_SSN but no change and always is null, what is your suggestion? what is the correct name for MainModel.SSN in action argument?

3条回答
Juvenile、少年°
2楼-- · 2019-03-21 15:22

Another, slightly more precise, take on Rahul's answer:

public JsonResult IsValidImageUrl(string SSN) {
    if (string.IsNullOrEmpty(SSN)) {
        string parmname = Request.QueryString.AllKeys.FirstOrDefault(k => k.EndsWith(".SSN"));
        if (!string.IsNullOrEmpty(parmname)) {
            SSN = Request.QueryString[parmname];
        }
    }

    //.... etc.

This can fly with multiple parameters.

Side note, you probably want to reconsider the association of a "GET" JSON method with anything associated with SSN's.

查看更多
何必那么认真
3楼-- · 2019-03-21 15:26

I resolved this issue by just using the first query string parameter:

public JsonResult IsValidImageUrl(string value) {

    if (value == null && Request.QueryString.Count == 1) {
            value = Request.QueryString[0];
        }


    //....            

    return Json(result, JsonRequestBehavior.AllowGet);                    
}
查看更多
我想做一个坏孩纸
4楼-- · 2019-03-21 15:36

You could try specifying a prefix:

public Action IsValidaSSN([Bind(Prefix = "MainModel")] string SSN) 
{
    //....
    return Json(result, JsonRequestBehavior.AllowGet);
}

MainModel is the prefix that is used to send the data => MainModel.SSN.

查看更多
登录 后发表回答