2016-09-15 80 views
0

我正在開發一個asp.net MVc核心應用程序。我有這樣的表單元素的彈出:html.hidden未在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)  
    } 

我有一個這樣的視圖模型:

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

但是看到視圖源,值是NULL

我嘗試使用:

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

,它顯示m_newIVR.Account填充。

然後我張貼的形式控制器這個動作

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

雖然我看到ACCOUNTID的視圖(使用viewsource)填充,但在後model.m_newIVR.Account的操作方法值爲空。

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是設置值,但爲什麼它不能在後處理方法中訪問?

請建議。

感謝

+0

使用'新{@值= Model.accountID}'決不設置'value'屬性。它不清楚你想要綁定什麼,但是ir應該是'@ Html.HiddenFor(m => m.m_newIVR.Account)',並且在將模型傳遞給控制器​​方法之前設置'Account'的值風景。 –

+0

而您的'Html.Hidden()'不起作用,因爲它會生成'name =「m.m_newIVR.Account」'並且您的模型不包含名爲'm'的屬性 –

+0

正在使用部分視圖?什麼@model在頁面頂部?我懷疑是因爲對於你說Model.AccountId的值,並且在隱藏中你使用了不同的模型。我試過它在我的模型中都適合我。 – dotnetstep

回答

0

現在我可以回答你的問題,爲什麼它的工作原理爲Html.Hidden而不是Html.HiddenFor。

  1. 當你Html.HiddenFor與M => m.m_newIVR.Account那麼它總是試圖隱藏字段值在任何可利用m.m_newIVR.Account不是值,可以指定使用@value值設定值= Model.AccountId。

如果你想使用HiddenFor在ViewModel中設置m_newIVR.Account只需使用下面的東西。

@Html.HiddenFor(m =>m.m_newIVR.Account) 
  • Html.Hidden不是強類型,以便不依賴於名稱。您可以指定不同的名稱和值參數。在這種情況下,您有責任爲HiddenField生成適當的名稱。
  • 我的工作樣品

    型號

    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" /> 
    } 
    

    //樣品從

    enter image description here

    +0

    我嘗試了@ Html.HiddenFor(m => m.m_newIVR.Account)並在控制器操作中設置了m_newIVR.Account,但仍未設置其值。可能是什麼原因? –

    +0

    你能否提供那個返回該視圖的Action? – dotnetstep

    +0

    我已添加我的工作示例。確保它將綁定到Account not AccountId。 – dotnetstep