0

我有一个问题,我创建了一个控制器和一个视图,用于从特定模型添加新项目。视图看起来像:

@modelModels.UserItem

@{
    ViewBag.Title = "New";
}

<h2>New</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)  
    <fieldset>
        <legend>Device</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.name)
            @Html.ValidationMessageFor(model => model.name)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

和控制器:

[HttpPost]
public ActionResult New(UserItem useritem)
{
    if (ModelState.IsValid)
    {
        db.UserItems.AddObject(useritem);
        db.SaveChanges();

        return RedirectToAction("Index");
    }

    return View(useritems);
}

我想如何在视图中向表单添加下拉列表,如下所示:

<select id="Select1">
    <option>MARS</option>
</select>

在控制器中提交后如何从表单中访问数据?

4

3 回答 3

6

为您的页面提供视图模型,此视图模型将在您的视图中使用。因此,仅包含模型中您真正需要的字段。在获取操作中,您应该创建此视图模型并从模型中获取所需的属性并将它们映射到您的视图模型。

public class UserItemViewModel
{
    /* Properties you want from your model */
    public string Property1                           { get; set; }
    public string Property2                           { get; set; }
    public string Property3                           { get; set; }

    /* Property to keep selected item */
    public string SelectedItem                        { get; set; }
    /* Set of items to fill dropdown */
    public IEnumerable<SelectListItem> SelectOptions  { get; set; }

    /* Fill the SelectListHere. This will be called from index controller */
    public void FillOptions()
    {
        var items = new[] { "Mars", "Venus" }.
              Select(x => new SelectListItem { Value = x, Text = x });

        SelectOptions= new SelectList(items, "Value", "Text");
    }
}

更改控制器以接收 ViewModel 而不是 Model 本身。

[HttpPost]
public ActionResult New(UserItemViewModel useritem)
{
    /* Repopulate the dropdown, since the values are not posted with model. */
    userItem.FillOptions();

    if (ModelState.IsValid)
    {
        /* Create your actual model and add it to db */

        // TODO: Map your properties from model to view model.
        // Let's say you created a model with name userItemModel
        db.UserItems.AddObject(userItemModel);
        db.SaveChanges();

        return RedirectToAction("Index");
    }
    return View(useritem);
} 

您可能需要稍微更改索引视图控制器。(填充下拉菜单)

[HttpGet]
public ActionResult Index()
{
    /* Create new viewmodel fill the dropdown and pass it to view */
    var viewModel = new UserItemViewModel();
    viewModel.FillOptitons();

    //TODO : From your model fill the required properties in view model as I mention.

    return View(viewModel);
} 

而你的看法,

/* Strongly typed view with viewmodel instead of model itself */
@modelModels.UserItemViewModel

/* This is the dropdown */
@Html.DropDownListFor(m => m.SelectedItem, Model.SelectOptions)
于 2012-07-02T07:49:26.917 回答
0
  1. 将该属性添加到您的模型中
  2. 使用内置EditorFor(首选)或手写 html 为该属性生成客户端输入。
  3. 通过在用户提交表单时检查该属性来访问提交的值
于 2012-07-02T07:45:55.783 回答
0

我喜欢 emre 关于拥有 viewModel 的提议,我认为这是解决您问题的最佳方法,但是以防万一您不想那样做(您必须有一个非常好的理由,因为它是最好的)并且仍然想要一种方法直接访问表单的值,您可以随时使用: var x = Request["myFiledName"]; 在控制器内部获取表单传递的值。

于 2012-07-02T08:07:17.047 回答