远程操作方法返回时绑定模型

时间:2014-11-13 04:04:12

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 spring-mvc model-view-controller

这是我的财产

[Remote("IsUserAlreadyExist","Admin",AdditionalFields="User_Id,NT_Login_Name",ErrorMessage=Constants.ErrorMessage.UserAlreadyExists)]
public string NT_Login_Name { get; set; }

动作方法如下: -

public JsonResult IsUserAlreadyExist(UserModel umodel)
{
  CommonAdapter commonAdapter = new CommonAdapter();
  Dictionary<string, object> spParameters = new Dictionary<string, object>();            
  spParameters.Add("inNTLogin", umodel.NT_Login_Name);
  DataSet userdetails = commonAdapter.ExecuteSP(Constants.SPName.GetUserByNTLogin, spParameters);
  if (userdetails != null)
  {
    if (userdetails.Tables[0].Rows.Count > 0)
    {
      if (umodel.User_Id > 0)
      {
        umodel.IsUserExists = true;
      }
      else
      {
        umodel.IsUserExists = false;
      }
    }
    else
    {
      IsUserOnLDAP(umodel);
      umodel.IsUserExists = true;
    }
  }
  return Json(umodel,JsonRequestBehavior.AllowGet);
}

模型返回正确的值但在我的视图中的文本框中没有绑定,如此

<td class="tds">
  @Html.TextBoxFor(x => x.NT_Login_Name, new { @id = "txtNT",     @class = "txtbox" })
  @Html.ValidationMessageFor(x => x.NT_Login_Name, "", new { @class = "errormessage", @validationgroup = "btnSubmit" })
</td>

1 个答案:

答案 0 :(得分:0)

Remote属性的目的是执行一些操作并以json格式返回布尔值或字符串

但在你的情况下,你正在返回一个模型

return Json(umodel,JsonRequestBehavior.AllowGet);

我猜你必须修改你的代码才能返回

return Json(true, JsonRequestBehavior.AllowGet);

如果验证成功并且

return Json("Some String", JsonRequestBehavior.AllowGet);

如果验证失败。

像这样的东西

public JsonResult IsUserAlreadyExist(UserModel umodel)
{
  CommonAdapter commonAdapter = new CommonAdapter();
  Dictionary<string, object> spParameters = new Dictionary<string, object>();            
  spParameters.Add("inNTLogin", umodel.NT_Login_Name);
  DataSet userdetails = commonAdapter.ExecuteSP(Constants.SPName.GetUserByNTLogin, spParameters);
  if (userdetails != null)
  {
    if (userdetails.Tables[0].Rows.Count > 0)
    {
      if (umodel.User_Id > 0)
      {
        //umodel.IsUserExists = true;
        return Json(false,JsonRequestBehavior.AllowGet);
      }
      else
      {
        //umodel.IsUserExists = false;
        return Json(true,JsonRequestBehavior.AllowGet);
      }
    }
    else
    {
      //IsUserOnLDAP(umodel);
      //umodel.IsUserExists = true;
      return Json(!IsUserOnLDAP(umodel),JsonRequestBehavior.AllowGet);
    }
  }
  return Json(false,JsonRequestBehavior.AllowGet);
}