html.hidden没有在asp.net MVC核心Razor视图中设置的值

时间:2016-09-15 01:15:22

标签: c# asp.net asp.net-mvc asp.net-core

我正在开发一个asp.net MVc核心应用程序。我有一个带有这样的表单元素的弹出窗口:

@using (Html.BeginForm("AddIVR", "ITPVoice", FormMethod.Post, new { role = "form" }))
 {    
       @*@Html.HiddenFor(m =>m.m_newIVR.Account, new { @value= Model.accountID})*@
       @Html.Hidden("m.m_newIVR.Account", Model.accountID)    
 }

我有一个像这样的viewmodel:

public class AccountDetailsViewModel
{
    public IVRS m_newIVR { get; set; }
}

和IVRS模型类如下:

 public class IVRS
 {

        [JsonProperty("_id")]
        public string Id { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("description")]
        public string Description { get; set; }

        [JsonProperty("account")]
        public string Account { get; set; }

}

我试图在我看来填充它:

@Html.HiddenFor(m =>m.m_newIVR.Account, new { @value= Model.accountID})

但是当我看到视图源时,Value为null

我尝试使用:

 @Html.Hidden("m.m_newIVR.Account", Model.accountID)

并显示填充了m_newIVR.Account。

然后我将表单发布到控制器此操作

 [HttpPost]       
        public ActionResult AddIVR(AccountDetailsViewModel model)
{
 return RedirectToAction("AccountDetails", "mycontroller")
}

虽然我看到AccountId在视图中填充(使用viewsource),但在post action方法中,model.m_newIVR.Account的值为null。

HTML输出如下所示:

        <div id="edit-ivrs-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">


      <div class="modal-dialog">
<form action="/ITPVoice/AddIVR" method="post" role="form">                        <div class="modal-content">
                            <div class="modal-header">

                                <h4 class="modal-title">Add IVR</h4>
                                <input id="m_newIVR_Account" name="m_newIVR.Account" type="hidden" value="" />
                                <input id="AccountId" name="AccountId" type="hidden" value="56f5e3d77ea022a042665be1" />
                            </div>
                            <div class="modal-body">
</div>
</div>

我的问题是:

  1. 为什么html.hiddenfor没有设置模型变量的值?
  2. 虽然html.hidden是设置值,为什么在post action方法中无法访问它?
  3. 请建议。

1 个答案:

答案 0 :(得分:1)

现在我能够回答你的问题为什么它适用于Html.Hidden而不适用于Html.HiddenFor。

  1. 当您使用m =&gt; m.m_newIVR.Account的Html.HiddenFor时,它总是尝试为属性m.m_newIVR.Account中可用的任何值设置隐藏字段值的值,而不是使用@value = Model指定的值。帐户ID。
  2. 如果你想在ViewModel中使用HiddenFor设置m_newIVR.Account,只需使用以下内容。

     @Html.HiddenFor(m =>m.m_newIVR.Account)
    
    1. Html.Hidden不是强类型,所以它不依赖于名称。您可以指定不同的名称和值参数。在这种情况下,您有责任为HiddenField生成正确的名称。
    2. 我的工作样本

      <强>模型

      public class AccountDetailsViewModel
      {
          public string AccountId { get; set; }
          public IVRS m_newIVR { get; set; }
      }
      
      public class IVRS
      {
      
          [JsonProperty("_id")]
          public string Id { get; set; }
      
          [JsonProperty("name")]
          public string Name { get; set; }
      
          [JsonProperty("description")]
          public string Description { get; set; }
      
          [JsonProperty("account")]
          public string Account { get; set; }
      
      }
      

      控制器操作

          [HttpGet]
          public IActionResult Index1()
          {
              AccountDetailsViewModel model = new AccountDetailsViewModel();
              //model.AccountId = "1222222";
              model.m_newIVR = new IVRS();
              model.m_newIVR.Account = "122222";
              return View(model);
          }
      
          [HttpPost]
          public IActionResult Index1(AccountDetailsViewModel model)
          {
              return View(model);
          }
      

      查看(Index1.cshtml)

      @model WebApplication2.Controllers.AccountDetailsViewModel
      
      @using (Html.BeginForm())
      {
          @Html.HiddenFor(m =>m.m_newIVR.Account)    
          <input type="submit" />
      }
      

      // Sample Out

      enter image description here