[英]How can I use selected value (not model value) as Post Parameter In Razor View?
I am posing a model back to my controller, 我正在将模型摆回我的控制器,
How every I want the user to select the country from a drop down, So I pass this to the view in a separate list: 我希望用户如何从下拉列表中选择国家,所以我将其传递给单独列表中的视图:
Controller GET: 控制器GET:
public static List<String> provinces = new List<String>() { "Eastern Cape", "Free State", "Gauteng", "KwaZulu-Natal", "Limpopo", "Mpumalanga", "Northern Cape", "North West", "Western Cape" };
public static List<String> countries = new List<String>() { "South Africa" };
public ActionResult Create()
{
ViewBag.Provinces = provinces.Select(x =>
new SelectListItem()
{
Text = x.ToString()
});
ViewBag.Countries = countries.Select(x =>
new SelectListItem()
{
Text = x.ToString()
});
return View();
}
Now I display the Drop down in the View: 现在,我在视图中显示下拉列表:
....
<p>
@Html.LabelFor(model => model.Province, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Provinces", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Province, "", new { @class = "text-danger" })
</div>
</p>
<p>
@Html.LabelFor(model => model.Country, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Countries",null , htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Country, "", new { @class = "text-danger" })
</div>
</p>
....
and here is my View Model: 这是我的视图模型:
public class RadioNetwork
{
public int NetworkIdentifier { get; set; }
public string CommonName { get; set; }
public string RepeaterName { get; set; }
public string StartCode { get; set; }
public string Frequency { get; set; }
public string Area { get; set; }
public string Province { get; set; }
public string Country { get; set; }
}
So I have discovered where the issue is. 因此,我发现了问题所在。 But I donot know how to solve it.
但我不知道如何解决。
I need the form to take the selected Value of my DropDown and not of the model? 我需要表格接受我的DropDown而不是模型的选定值吗?
Because when I receive the View Model in the POST Controller the provence and Country are `Null' 因为当我在POST控制器中收到视图模型时,普罗旺斯和国家/地区为“空”
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "NetworkIdentifier,CommonName,RepeaterName,StartCode,Frequency,Area,Province,Country")] RadioNetwork radioNetwork)
{
Try 尝试
@Html.DropDownList(m => m.Province, (SelectList)ViewBag.Provinces, ...
This will bind the selected Province to your model 这会将选定的省绑定到您的模型
Your model isn't binding properly because you're using the wrong method to render the DropDownLists. 您的模型未正确绑定,因为您使用了错误的方法来渲染DropDownLists。 The fields in your form bind to your model according to the
name
attribute, and the DropDownList()
method renders a field without that attribute. 表单中的字段根据
name
属性绑定到模型,并且DropDownList()
方法呈现不带该属性的字段。 When you want form fields to bind properly to a model, you should always take care to use methods that end in For
so you don't have to worry about the model binding. 当您希望表单字段正确地绑定到模型时,应始终注意使用以
For
结尾的方法,因此您不必担心模型绑定。
You're doing 你在做
@Html.DropDownList("Countries",null , htmlAttributes: new { @class = "form-control" })
You should be doing 你应该在做
@Html.DropDownListFor(model => model.Country, (List<SelectListItem>)ViewBag.Countries, new { @class = "form-control" })
Also, your lists should be: 另外,您的清单应为:
ViewBag.Provinces = provinces.Select(x =>
new SelectListItem()
{
Text = x.ToString(),
Value = x.ToString()
}).ToList();
PS You don't need any of the bind stuff at all. PS:您根本不需要任何绑定内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.