简体   繁体   English

Razor @ Html.DropDownListFor提交并在服务器上获取选项值

[英]Razor @Html.DropDownListFor submit & get the option value on the server

I am trying to submit when a dropdownlist changes by doing this 我正在尝试通过下拉列表更改提交

@using (Html.BeginForm("testAction", "FishingTrip"))
{
    @Html.DropDownListFor(x => x.Day, Model.Days,  new { onchange="this.form.submit();" })
}

This works fine but I am having problems (in other words don't know how) to get the option value on the server, can anybody help me with this ? 这可以正常工作,但是我在获取服务器上的选项值时遇到问题(换句话说,不知道如何操作),有人可以帮助我吗?

cheers sushiBite 干杯寿司

You could simply have your POST controller action take it as parameter and leave the default model binder to the binding: 您可以简单地让POST控制器操作将其作为参数,并将默认的模型绑定器保留在绑定中:

[HttpPost]
public ActionResult TestAction(string day)
{
    // The day parameter will contain the selected value
    ...
}

Or directly use the view model you used in the view: 或直接使用您在视图中使用的视图模型:

[HttpPost]
public ActionResult TestAction(MyViewModel model)
{
    // The model.Day parameter will contain the selected value
    ...
}

就像其他任何属性/编辑器一样,只需使用model参数的Day属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM