繁体   English   中英

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

[英]How can I use selected value (not model value) as Post Parameter In Razor View?

我正在将模型摆回我的控制器,

我希望用户如何从下拉列表中选择国家,所以我将其传递给单独列表中的视图:

控制器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();
    }

现在,我在视图中显示下拉列表:

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

这是我的视图模型:

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

因此,我发现了问题所在。 但我不知道如何解决。

我需要表格接受我的DropDown而不是模型的选定值吗?

因为当我在POST控制器中收到视图模型时,普罗旺斯和国家/地区为“空”

   [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create([Bind(Include = "NetworkIdentifier,CommonName,RepeaterName,StartCode,Frequency,Area,Province,Country")] RadioNetwork radioNetwork)
    {

尝试

@Html.DropDownList(m => m.Province, (SelectList)ViewBag.Provinces, ...

这会将选定的省绑定到您的模型

您的模型未正确绑定,因为您使用了错误的方法来渲染DropDownLists。 表单中的字段根据name属性绑定到模型,并且DropDownList()方法呈现不带该属性的字段。 当您希望表单字段正确地绑定到模型时,应始终注意使用以For结尾的方法,因此您不必担心模型绑定。

你在做

@Html.DropDownList("Countries",null , htmlAttributes: new { @class = "form-control" })

你应该在做

@Html.DropDownListFor(model => model.Country, (List<SelectListItem>)ViewBag.Countries, new { @class = "form-control" })

另外,您的清单应为:

ViewBag.Provinces = provinces.Select(x =>
                          new SelectListItem()
                          {
                              Text = x.ToString(),
                              Value = x.ToString()
                          }).ToList();

PS:您根本不需要任何绑定内容。

暂无
暂无

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

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