2013-07-26 82 views
2

在我的控制,我有以下代碼:Url.Action不傳遞參數指標

public ActionResult Index(int? videoId, int languageId = 0) 
     { 

       //Some code 
     } 

然後在我的.chtml頁我引用它,像這樣:

@using (Html.BeginForm("Index", "VideoLanguage", FormMethod.Post)) 
{ 
    @Html.Hidden("videoId", Model.VideoId) 
    @Html.Hidden("videoLanguageId", Model.SelectedLanguage.LanguageId) 

    <div> 
     @Html.DropDownListFor(model => model.SelectedLanguage.LanguageId, ViewData["LanguageId"] as SelectList) 
    </div> 
    <div> 
      <a href="@Url.Action("Index", "VideoLanguage",new { videoId = Model.VideoId, languageId = Model.SelectedLanguage.LanguageId })" class="linkbutton">Add Language</a> 
    </div> 

... code keeps going 

會發生什麼時我點擊按鈕,從下拉列表中選擇的LanguageId值不會被傳遞。原始值是。

+0

你不這樣做是正確的。你能在這裏發佈你的模型的代碼嗎?如果你這樣做,我會告訴你如何以正確的方式做到這一點。 – ataravati

回答

1
<a href="@Url.Action("Index", "VideoLanguage",new { videoId = Model.VideoId, languageId = Model.SelectedLanguage.LanguageId })" class="linkbutton">Add Language</a> 

是當頁面加載,所以在HTML中的值將被硬性規定,你需要做的是無論是在JavaScript設置的超級鏈接參數或使用:

<input type="submit" value="Index" /> 

How to pass a textbox value from view to a controller in MVC 4?

+0

這個答案幫助我瞭解問題所在。謝謝。 –

0

試試這個:

@using (Html.BeginForm("Index", "VideoLanguage", FormMethod.Post, new { videoId = Model.VideoId, languageId = Model.SelectedLanguage.LanguageId })) 
{ 
    .... 
} 
0

你應該通過您的PARAMS到@Html。 BeginForm幫手,只是提交表單

@using (Html.BeginForm("Index", "VideoLanguage", FormMethod.Post, new { videoId = Model.VideoId, languageId = Model.SelectedLanguage.LanguageId })) 
{ 
    @Html.Hidden("videoId", Model.VideoId) 
    @Html.Hidden("videoLanguageId", Model.SelectedLanguage.LanguageId) 

    <div> 
     @Html.DropDownListFor(model => model.SelectedLanguage.LanguageId, ViewData["LanguageId"] as SelectList) 
    </div> 
    <div> 
     <input type="submit" value="Add Language"/> 
    </div> 

... code keeps going 
} 

在你的控制器,你也應該有兩種方法獲取和使用相同的名稱設置:

public ActionResult Index() 
     { 
//Here just get a view 
     } 

[HttpPost] 
public ActionResult Index(int? videoId, int languageId = 0) 
     { 
//Here code to save in database 
     }