如何在重定向和触发按钮提交时填充不同的视图

时间:2015-04-03 18:15:35

标签: javascript c# jquery asp.net asp.net-mvc

我正在使用MVC / ASP.NET

我有两个视图,一个显示基于组ID的项目列表。另一页显示从多个选项生成的各种信息。用户可以输入的选项由下拉选择确定。

View1.aspx

<input type="button" 
       value="View All Details%>" 
       class="button" 
       onclick="window.location = '<%:Url.Action("Index", "Report", new { id = Model.GroupID})%>'" />

我能够成功切换到指示的页面并调用页面的控制器。 Model.GroupID是我提交新表单所需的值。

Q1 :如何触发下拉框转到所需的选定索引,然后使用“Model.GroupID”中的值填充文本框?

的Index.aspx

...
<% using (Html.BeginForm("Report", "Report", FormMethod.Post, new { id = "some" }))
{
      ....
      <%: Html.TextBoxFor(model => model.textBox.input, new { id = "textboxID", style = "width:" + Model.textBox.width + "px", maxlength = Model.textBox.maxLength })%>

      <input type="submit" name="button" value="Find Details" style="width: 180px" />
 }

我需要提交表单,该表单将调用我的控制器

Controller.cs

[Authorize]
[HttpPost]
public ActionResult Report([Bind(Include = "textBox")]Report values)
{
    ....
}

有没有办法直接从不同的视图中调用?我不介意在控制器中创建一个单独的方法来避免绑定....

1 个答案:

答案 0 :(得分:0)

用于触发下拉列表以选择您可以执行的特定项目:

@Html.DropDownListFor(model => model.GroupID, new SelectList((System.Collections.IEnumerable)ViewData["GroupsAvailable"], "Value", "Text", Model.GroupID))

但您还必须在GroupsAvailable或模型内传递ViewData

你可以通过ViewData

传递它

IEnumerable<SelectListItem> availableGroups = db.Groups .Where(g => g.IsActive) // for example .OrderByDescending(c => c.Id) .Select(c => new SelectListItem { Value = c.Id.ToString(), Text = c.GroupName }); ViewData["GroupsAvailable"] = availableGroups.ToList();

但总而言之,你不清楚你想要回答第二个问题的目的是什么。