将存储在按钮“value property”中的值传递给我的控制器

时间:2014-09-08 19:51:06

标签: asp.net-mvc asp.net-mvc-4

我试图传递存储在按钮中的值" 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>

1 个答案:

答案 0 :(得分:0)

问题中显示的代码将普通表单发布与JavaScript重定向混合在一起。让我们简化一下方法:

  • 创建一个包含在Html.BeginForm { ... }
  • 中的表单
  • 将表单的操作设置为MVC控制器和将处理college参数的操作。在您的代码中,控制器看起来像AllTracksController,而操作是ListTracksByMajor。问题并不完全清楚,但我猜测你的collegeOf参数是一个表示学院显示名称或类似字段的字符串。如果没有,请调整这些说明以适合您的数据类型。
  • 在Action方法签名中,您将接受在表单上各个按钮的value属性中设置的输入参数(如果它是一个字符串,那么您需要一个字符串输入参数)。
  • 然后在Action方法中,您将根据需要验证输入参数,然后使用它来查找并加载您想要的任何模型&#34;按主要列出曲目&#34;图。
  • 然后按主要提供&#34; List Tracks&#34;用模型填充的视图。

下面是一些演示这些概念的示例代码。同样,我对您的用例做了一些任意的假设,但这证明了这种方法:

模型

ExampleCollegeModel

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; }
}

ExampleTracksByMajorModel

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