回发到视图后,值不会改变?

jpe*_*rez 2 http-post asp.net-mvc-3

编辑:我已经改变了我的问题和代码,以便更好地澄清我的问题

我有这个(强类型视图)确实使用控制器中为该特定模型提供的值

我想从另一个模型中添加一些模型,在从我的httpPost回复后回复没有任何反应...

提前致谢!

--------------------------------------其他代码更清楚地澄清我的问题--- -

 public class Address
 {
public int Id { get; set;}
public String Name { get; set;}
 }

 public class OtherAddress
 {
public int Id { get; set;}
public String Name { get; set;}
public String City { get; set;}
 }

 public class MasterModel
{
 public Address Address { get; set;}
 public List<OtherAddress> OtherAddressess { get; set;}
 }



 public ActionResult Create()
 {
 MasterModel Model = new MasterModel();
 Model.Person = new Person();
 Model.Address = new Address();
 Model.OtherAdressess = new List<OtherAddress>();

 DBContext _db = new DBContext();
 Model.OtherAdressess = _db.OtherAddressess.Where(a=> a.City == "Amsterdam");
 return View(Model);
 }
Run Code Online (Sandbox Code Playgroud)

在视图中

    @model Project.Models.MasterModel

    List<SelectListItems> items = new List<SelectListItems>();

    foreach(var a in Model.OtherAddressess)
    {
        SelectListItem item = new SelectListItem();
        item.Value = a.Id.toString();
        item.Text = a.Street;
    }

    @using (@Html.BeginForm())
    {
    <div>
    <select name="otheraddress">
    foreach(var i in Items)
    {
        <option value=@i.Value>@i.Text</option>
    }
    </select>
    <input type="submit" name="select" value="Select Address"/>
    </div>
    <div>
    @Html.EditorFor(model => Model.Address.Name)
    <div>
    <p>
    <input type="submit" value="Submit"/>
    </p>
    }
Run Code Online (Sandbox Code Playgroud)

在邮寄

    [HttpPost]
    public ActionResult Create(MasterModel Model)
    {
    String otherAddressSelected = Request.Params["select"];
    if(!String.IsNullOrEmpty(otherAddressSelected))
    {
        int id = int.Parse(Request.Params["otheraddress"]);
        DBContext _db = new DBContext();
        OtherAddress oa = _db.OtherAddress.Single(oa=> oa.Id == id);
        Model.Address.Name = oa.Name;
        return View(Model);
    }
    //other stuff here
    }
Run Code Online (Sandbox Code Playgroud)

jpe*_*rez 6

如果要在[HttpPost]控制器中更改模型的值,则必须删除要更改的实例/属性的modelstate.例如:

  [HttpPost]
  public ActionResult Index(SomeModel model)
  {
     ModelState.Remove("Name");
     model.Name = "some new name";
      return View(model);
  }
Run Code Online (Sandbox Code Playgroud)

得到了这个例子的答案