如何发布一个选择列表使用视图模型控制器的选择的价值? [英] How to post the selected value of a selectlist to the controller using a view model?

查看:101
本文介绍了如何发布一个选择列表使用视图模型控制器的选择的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经被问以各种形式,但没有一个答案似乎适合我的情况。我只是想找回我的控制器下拉列表中选择的值。

This question has been asked in various forms but none of the answers seem to fit my situation. I am simply trying to retrieve the selected value of a dropdown list in my controller.

下面是我的code:

ViewModel.cs

ViewModel.cs

public class ViewModel
{
  public ViewModel() {}
  public ViewModel(Contact contact, IEnumerable<State> states)
    {
      this.Contact = contact;
      this.States = new SelectList(states, "Id", "Name", contact.StateId);
    }
  public Contact Contact {get;set;}
  public SelectList States {get;set;}
}

Controller.cs

Controller.cs

[HttpPost]
public ActionResult Edit(ViewModel viewModel)
{
  _contactService.UpdateContact(viewModel.Contact);
  return RedirectToAction("Item", new {id = viewModel.Contact.Id});
}

View.cshtml

View.cshtml

<button type="submit" onclick="javascript:document.update.submit()"><span>Update</span></button>//aesthic usage. 
@{using (Html.BeginForm("Edit", "Controller", FormMethod.Post, new { name = "update" }))
  {
   @Html.HiddenFor(m => m.Contact.Id)
   @Html.LabelFor(m => m.Contact.Name, "Name:")
   @Html.TextBoxFor(m => m.Contact.Name)

   <label for="state">State:</label>
   @Html.DropDownList("state", Model.States)
}
}

一切正常,只是没有从DropDownList的值在我贴的ViewModel传递给控制器​​。编辑页面,所有字段加载正确。在下拉菜单正确绑定,并正确显示其选择的值。然而,当我后我只得到传递给控制器​​联系方式的对象。在国家的SelectList对象为null。

Everything works as expected except that no values from the dropdownlist are passed in my posted viewModel to the controller. The edit page and all fields load correctly. The dropdowns bind correctly and have their selected values displayed properly. However, when I post I only get a "Contact" object passed to the controller. The "States" SelectList object is null.

我想在我的ViewModel contstructor映射STATEID的属性,但没有任何工作。我在做什么错了?

I tried mapping a "StateId" property in my viewModel contstructor but that did not work either. What am I doing wrong?

感谢。

推荐答案

我不喜欢回答我自己的问题,但根据我加上现有的答案的无数我以为我会总结我的发现了多个问题。

I hate answering my own questions but based on the multiple issues I had coupled with the myriad of answers available I thought I would summarize my findings.

首先感谢菲利普,他的回答并没有完全解决我的问题,但它使我在正确的方向。 +1

First off thanks for Filip, his answer did not exactly fix my problem but it led me in the right direction. +1

如果您正在创建用于查看和编辑方式需要一个下拉列表,这里有一些建议和陷阱。我将与我需要适合我的需要的参数列表中开始。

If you are creating a form for viewing and editing that requires a drop down list, here are some suggestions and gotchas. I will start with a list of parameters that I needed to fit my needs.


    在我看来
  1. 强类型的意见是preferable。最小化神奇的字符串。

  2. 查看模型应包含尽可能少的逻辑和不相干的内容成为可能。目前唯一的工作应该是促进数据对象的集合。

  3. 的下拉列表中应显示所选值。

  4. 选择的值应该很容易映射回表格视图模型提交。

这听起来像一个明显和容易得到的名单,但对于某人来说新的MVC,事实并非如此。我会从上面的意见修改我的code。下面是我做的。

This may sound like an obvious and easily obtainable list but for someone new to MVC, it is not. I will revise my code from above with comments. Here is what I did.

ViewModel.cs

ViewModel.cs

public class ViewModel
{
  public ViewModel() {}
  public ViewModel(Contact contact, IList<State> states)
  {
//no need to pass in a SelectList or IEnumerable, just what your service or repository spits out
    this.Contact = contact;
    this.States = states;
  }
  public Contact Contact {get;set;}
  public IList<State> States {get;set;}
}

Controller.cs //没有什么比上面

Controller.cs //nothing really different than above

public ActionResult Edit(int id)
{
  var contact = _contactService.GetContactById(id);
  var states = _stateService.GetAllStates();
  return View(new ViewModel(contact, states));
}

public ActionResult Edit(ViewModel viewModel)
{
  _contactService.UpdateContact(viewModel.Contact);
  return RedirectToAction("Edit", new {id = viewModel.Contact.Id });
}

查看//感谢送给这个<一来Artirto href=\"http://stackoverflow.com/questions/1916462/dropdownlistfor-in-editortemplate-not-selecting-value\">post

@{using (Html.BeginForm("Edit", "Controller", FormMethod.Post))
 {
  @Html.HiddenFor(m => m.Contact.Id)
  @Html.DropDownListFor(m => m.Contact.StateId, new SelectList(Model.States, "Id", "Name", @Model.Contact.StateId))
<input type="submit" value="Save" />
}
}

这篇关于如何发布一个选择列表使用视图模型控制器的选择的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆