【问题标题】:Bind the model when remote action method returns远程操作方法返回时绑定模型
【发布时间】:2014-11-13 04:04:12
【问题描述】:

这是我的财产

[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>

【问题讨论】:

  • 返回truefalse,而不是模型。 return Json(true, JsonRequestBehavior.AllowGet);
  • IsUserAlreadyExist 调用后如何加载/分配视图一次?如果您使用...,您能否也显示该脚本?
  • @RJK,它的jquery.validate.unobtrusive.js :)

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


【解决方案1】:

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);
}

【讨论】:

  • 不需要返回消息(它已经在属性中指定)并且没有必要设置 umodel.IsUserExists 的值 - 它没有被保存在任何地方,所以它会尽快丢失并且方法返回(无论如何,属性似乎有点毫无意义)
  • 我明白你的意思。但至少你必须返回 Json(false,JsonRequestBehavior.AllowGet);显示默认消息。
  • 我知道 - 这就是我在 3 小时前的评论中说的原因 :)
  • 是的,设置umodel.IsUserExists的值没有意义。我只是重用了问题中的代码并添加了返回语句
  • 并且,返回 Json("Any Message",JsonRequestBehavior.AllowGet);返回 Json(false,JsonRequestBehavior.AllowGet);是一样的。因为不真实的东西都是假的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
  • 1970-01-01
  • 2011-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-29
相关资源
最近更新 更多