在我的控制器中,我有以下代码:
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 :(得分: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?
答案 1 :(得分:0)
试试这个:
@using (Html.BeginForm("Index", "VideoLanguage", FormMethod.Post, new { videoId = Model.VideoId, languageId = Model.SelectedLanguage.LanguageId }))
{
....
}
答案 2 :(得分:0)
你应该将你的参数传递给@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
}
在控制器中,您还应该有两个方法GET和SET具有相同的名称:
public ActionResult Index()
{
//Here just get a view
}
[HttpPost]
public ActionResult Index(int? videoId, int languageId = 0)
{
//Here code to save in database
}