2

在我的控制器中,我有以下代码:

    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 值没有传递。原值为。

4

3 回答 3

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

请参阅如何将文本框值从视图传递到 MVC 4 中的控制器?

于 2013-07-26T22:54:09.220 回答
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
        }
于 2013-07-28T19:51:43.843 回答
0

尝试这个:

@using (Html.BeginForm("Index", "VideoLanguage", FormMethod.Post, new { videoId = Model.VideoId, languageId = Model.SelectedLanguage.LanguageId }))
{
  ....
}
于 2013-07-27T16:42:04.447 回答