我试图传递存储在按钮中的值" value property"单击按钮时,我的控制器。我是asp.net mvc的新手,我不确定完成此任务的最佳方法是什么。我试着环顾论坛,但没有找到足够清楚的东西。我不想要答案,我希望能够理解它。感谢任何提示/提示/等。以下是我的按钮的代码:
<div class="jumbotronAll">
@foreach (var item in Model)
{
@Html.Raw("<br />")
Html.BeginForm("Index", "AllTracks");
{
<input type="button" value='@item.collegeOf' class="AllTracksButtons" onclick="location.href='@Url.Action("ListTracksByMajor", "AllTracks", new { college = item.collegeOf })'" />
}
@Html.Raw("<br />")
}
</div>
答案 0 :(得分:0)
问题中显示的代码将普通表单发布与JavaScript重定向混合在一起。让我们简化一下方法:
Html.BeginForm { ... }
college
参数的操作。在您的代码中,控制器看起来像AllTracksController
,而操作是ListTracksByMajor
。问题并不完全清楚,但我猜测你的collegeOf
参数是一个表示学院显示名称或类似字段的字符串。如果没有,请调整这些说明以适合您的数据类型。value
属性中设置的输入参数(如果它是一个字符串,那么您需要一个字符串输入参数)。下面是一些演示这些概念的示例代码。同样,我对您的用例做了一些任意的假设,但这证明了这种方法:
public class ExampleCollegeModel
{
/// <summary>
/// Preserving SO variable name, but note that normal convention on public
/// C# property is CollegeOf
/// </summary>
public string collegeOf { get; set; }
}
/// <summary>
/// Obviously I just made this up since I don't know what your data model should look like. You
/// may be wanting some kind of IEnumerable, etc., so change this to whatever it needs to be.
/// </summary>
public class ExampleTracksByMajorModel
{
public string PropertyA { get; set; }
public string PropertyB { get; set; }
public string PropertyC { get; set; }
}
public class AllTracksController : Controller
{
public ActionResult Index()
{
// Guessing this is the default view that shows the form in the question.
// You would go to the DB here and get your model loaded up. I assume from
// your question your view is expecting an array or some-such.
var colleges = new ExampleCollegeModel[]
{
new ExampleCollegeModel() {collegeOf = "College A"},
new ExampleCollegeModel() {collegeOf = "College B"},
new ExampleCollegeModel() {collegeOf = "College C"}
};
return View(colleges);
}
[HttpPost]
public ActionResult ListTracksByMajor(string collegeButton)
{
// Validate the input. Note this is simple manual validation of your string input, where you are checking for whatever
// you want. Another way to do this would be to accept an instance of a Model object that you pass in, and if your use System.ComponentModel.DataAnnotation attributes on your model, you can check ModelState.IsValid
// If the input is invalid, you could redirect back to a different Action / View, or you could provide a default value here so you can continue on your way.
// ....
// Now that you have valid input, go to data store and load up what yourever your proper model is for the view you're going to show. I'm guessing
// it's not too similar to the below contrived example :)
// ...
var model = new ExampleTracksByMajorModel
{
PropertyA = "Some values from the database",
PropertyB = "would be loaded here.",
PropertyC = "By the way, you selected: " + collegeButton
};
return View(model);
}
}
AllTracksController
- &gt; Index
操作)@model YourModelNameSpace.ExampleCollegeModel[]
@{
ViewBag.Title = "All Tracks - Choose college";
}
<div class="container">
<h4>Example form showing all possible college choices as individual buttons.</h4>
@using (Html.BeginForm("ListTracksByMajor", "AllTracks"))
{
@Html.AntiForgeryToken()
foreach (var item in Model)
{
<div class="row">
<div class="form-group">
<div>
<input type="submit" name="collegeButton" value='@item.collegeOf' class="btn-primary" />
</div>
</div>
</div>
}
}
</div>
AllTracksController
- &gt; ListTracksByMajor
操作)@model YourModelNameSpace.ExampleTracksByMajorModel
@{
ViewBag.Title = "List Tracks By Major - Example";
}
<h2>List TracksBy Major - based on chosen college input parameter</h2>
<div class="container">
<div class="row">
<dl class="dl-horizontal">
<dt>
Property A
</dt>
<dd>
@Html.DisplayFor(model => model.PropertyA)
</dd>
<dt>
Property B
</dt>
<dd>
@Html.DisplayFor(model => model.PropertyB)
</dd>
<dt>
Property C
</dt>
<dd>
@Html.DisplayFor(model => model.PropertyC)
</dd>
</dl>
</div>
</div>