简体   繁体   English

视图中的ASP.NET MVC绑定值

[英]ASP.NET MVC Binding Value in View

Struggling to get a value from a drop down, to be passed into my controller for db inserting. 努力从下拉列表中获取值,然后将其传递到我的控制器中以进行数据库插入。 It starts with a simple class for each item in the drop down. 它从下拉列表中的每个项目的简单类开始。

 public class selectListUrgencyNum
{
    public string Text { get; set; }
    public string Value { get; set; }
}

Then in my view model I create a list of these objects as such.. 然后在我的视图模型中创建这些对象的列表。

public class createViewModel
{
    public Change Changevm { get; set; }

    public List<RequestType> rTypes { get; set; }

    public List<selectListUrgencyNum> listUrgency { get; set; }
}

Finally is my controller I populate this list to be displayed in the dropdown. 最后是我的控制器,我将填充此列表以显示在下拉列表中。

public ActionResult Create()
    {

        var urgNums = new List<selectListUrgencyNum>();
        for(int i = 1; i < 6; i++)
        {
            urgNums.Add(new selectListUrgencyNum() { Text = i.ToString(), Value = i.ToString() });
        }

        var viewModel = new createViewModel
        {
            listUrgency = urgNums 
        };



        return View(viewModel);
    }

I just used 1 - 5 as to make it simpler to understand. 我只是使用1-5来简化理解。

My view creates the ddl like this. 我的视图将像这样创建ddl。

  <div class="form-group">
        @Html.LabelFor(model => model.Changevm.UrgencyNum, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-2">
            <select class="form-control" id="select">
                <option> 1 = Least Urgent</option>
                @foreach(var item in Model.listUrgency)
                {
                    <option>@item.Text</option>
                }
                </select>
             @*How do I grab the value of what is selected??*@
          @Html.ValidationMessageFor(model => model.Changevm.UrgencyNum, "", new { @class = "text-danger" })
         </div>

So I'm not sure how to get the VALUE of what the selected drop down option is, so that on my insert I can access it. 因此,我不确定如何获取所选下拉选项的值,以便在我的插入内容中可以访问它。 Possibly a hiddenfor? 可能是隐藏的?

And if I use a hidden for, how would it look? 如果我将其用作隐藏物,它将看起来如何?

If already answered please link.. 如果已经回答,请链接。

If you want to pass just Text and Value, you can use SelectListItem class from System.Web.Mvc, which is there for passing items to dropdown lists. 如果只想传递文本和值,则可以使用System.Web.Mvc中的SelectListItem类,该类用于将项目传递给下拉列表。

public class CreateViewModel
{
    public Change ChangeVm { get; set; }

    public List<RequestType> Types { get; set; }

    public List<SelectListItem> ListUrgency { get; set; }
}

In the Controller Create() method we create new ChangeVm also 在Controller Create()方法中,我们还创建了新的ChangeVm

var viewModel = new CreateViewModel()
{
    ChangeVm = new Change(),
    ListUrgency = urgNums
};

return View(viewModel);

On the View you can use Html.DropDownListFor extension method which will take care of creating the list with given items and proper name. 在视图上,您​​可以使用Html.DropDownListFor扩展方法,该方法将用于创建具有给定项目和专有名称的列表。

Html.BeginForm will take care of creating the form with proper "action" url, and method. Html.BeginForm将负责使用适当的“操作” URL和方法来创建表单。

@using (Html.BeginForm("Create", "Home", FormMethod.Post))
{
<div class="form-group">
    @Html.LabelFor(model => model.ChangeVm.UrgencyNum, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-2">
        @Html.DropDownListFor(model => model.ChangeVm.UrgencyNum, Model.ListUrgency, "Choose Urgency...")
        @Html.ValidationMessageFor(model => model.ChangeVm.UrgencyNum, "", new { @class = "text-danger" })
    </div>
    <input type="submit" value="Submit"/>
</div>
}

In the controller you can then use 然后,您可以在控制器中使用

    [HttpPost]
    public ActionResult Create(CreateViewModel vModel)
    {
        if(vModel?.ChangeVm?.UrgencyNum != null)
        {
            var chosenValue = vModel.ChangeVm.UrgencyNum;
        }

        return View(vModel);
    }

If everything goes well, you should have your value chosen in vModel.ChangeVm.UrgencyNum 如果一切顺利,则应该在vModel.ChangeVm.UrgencyNum中选择您的值

Add name attribute to your select . name属性添加到您的select

<select class="form-control" id="select" name="selectedUrgency">

And within the posted action of the controller, lets say we have Create2 action method. 并在控制器的已发布操作中,假设我们具有Create2操作方法。 We can retrieve value from Request object. 我们可以从Request对象检索值。

[HttpPost]
public ActionResult Create2()
{
   var value = Request["selectedUrgency"];
} 

Assuming that your given html has <form> tag with action url pointed to this action method. 假定您给定的html具有<form>标记,且操作url指向此操作方法。

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

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