简体   繁体   English

如何在Razor视图中使用选定值(而非模型值)作为后期参数?

[英]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.

相关问题 如何使用模型中的自定义属性来更改剃刀视图模型中的输入元素名称属性值? - How can I change the input element name attribute value in a razor view model using a custom attribute in a model? 如何在Razor视图中正确发布一个十进制值? - How do I properly post a decimal value in a Razor View? 如何在 Razor 中单击按钮时更改模型值? - How can i change my model value on button click in Razor? 如何在不使用表单帖子的情况下从视图中的 SelectList 获取选定的值到控制器? - How can I get the selected value from a SelectList in a view to the controller without using a form post? ASP.NET MVC 2-如何从下拉菜单中获取选定的值? - ASP.NET MVC 2 - How can I get the selected value from a drop down into my view model? MVC 3 Razor DropdownListFor-选择值后如何“过滤”视图 - MVC 3 Razor DropdownListFor - How to “filter” the view after value is selected DropDownList在剃刀视图页面上选择的值 - DropDownList selected value on a razor view page 如何将 Model 的选定属性绑定到 Razor 页面? - How can I bind selected properties of a Model to a Razor Page? 无法在剃刀页面中将模型值分配给参数 - Not able to assign model value to a parameter in razor pages 如何在MVC的Razor视图中更新Model值并重新加载div - How to update the Model value and reload a div in Razor view in MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM