2010-05-10 24 views
0

我将ViewModel(其中包含“Person”对象)从“EditPerson”控制器操作传递到视图中。当从视图返回时,ActionResult接收除ID之外的所有Person属性(它表示为零而不是其实数)ASP.NET。 MVC2。实体框架。无法将主键值从视图传递回[HttpPost]

有谁能告诉我为什么?

的控制器是这样的:

 public ActionResult EditPerson(int personID) 
    {   
     var personToEdit = repository.GetPerson(personID); 

     FormationViewModel vm = new FormationViewModel(); 

     vm.Person = personToEdit; 

     return View(vm); 
    } 

    [HttpPost] 
    public ActionResult EditPerson(FormationViewModel model) <<Passes in all properties except ID 
    { 
     // Persistence code 
    } 

观是这样的:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Afp.Models.Formation.FormationViewModel>" %> 
<fieldset> 
     <legend>Fields</legend> 


     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.Person.Title) %> 
     </div> 

     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.Person.Title) %> 
      <%= Html.ValidationMessageFor(model => model.Person.Title) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.Person.Forename)%> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.Person.Forename)%> 
      <%= Html.ValidationMessageFor(model => model.Person.Forename)%> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.Person.Surname)%> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.Person.Surname)%> 
      <%= Html.ValidationMessageFor(model => model.Person.Surname)%> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.DOB) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.DOB, String.Format("{0:g}", Model.DOB)) 
      <%= Html.ValidationMessageFor(model => model.DOB) %> 
     </div>--%> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.Person.Nationality)%> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.Person.Nationality)%> 
      <%= Html.ValidationMessageFor(model => model.Person.Nationality)%> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.Person.Occupation)%> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.Person.Occupation)%> 
      <%= Html.ValidationMessageFor(model => model.Person.Occupation)%> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.Person.CountryOfResidence)%> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.Person.CountryOfResidence)%> 
      <%= Html.ValidationMessageFor(model => model.Person.CountryOfResidence)%> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.Person.PreviousNameForename)%> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.Person.PreviousNameForename)%> 
      <%= Html.ValidationMessageFor(model => model.Person.PreviousNameForename)%> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.Person.PreviousSurname)%> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.Person.PreviousSurname)%> 
      <%= Html.ValidationMessageFor(model => model.Person.PreviousSurname)%> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.Person.Email)%> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.Person.Email)%> 
      <%= Html.ValidationMessageFor(model => model.Person.Email)%> 
     </div> 

     <p> 

      <input type="submit" value="Save" /> 

     </p> 
    </fieldset> 

而Person类的样子:

[MetadataType(typeof(Person_Validation))] 
public partial class Person 
{ 
    public Person() 
    { 
    } 
} 

[Bind(Exclude = "ID")] 
public class Person_Validation 
{ 
    public int ID { get; private set; } 
    public string Title { get; set; } 
    public string Forename { get; set; } 
    public string Surname { get; set; } 
    public System.DateTime DOB { get; set; } 
    public string Nationality { get; set; } 
    public string Occupation { get; set; } 
    public string CountryOfResidence { get; set; } 
    public string PreviousNameForename { get; set; } 
    public string PreviousSurname { get; set; } 
    public string Email { get; set; } 
} 

和视图模型:

public class FormationViewModel 
{ 

    public Company Company { get; set; } 

    public Address RegisteredAddress { get; set; } 

    public Person Person { get; set; } 

    public PersonType PersonType { get; set; } 

    public int CurrentStep { get; set; } 
} 

}

回答

1

从你的模型类删除

[Bind(Exclude = "ID")] 

+0

工作完成!谢谢! – 2010-05-10 08:43:54

1

的ID就不贴了,因为它不是在表单内。你可以把它作为一个隐藏字段:

<%= Html.HiddenFor(model => model.Person.ID) %> 
+0

不幸的是,不幸的是。我添加了它,但仍然获得传回的0 ID – 2010-05-10 07:33:43

+0

呈现表单的操作需要填充它。 – 2010-05-10 07:41:28

+0

不是“vm.Person = personToEdit;”在ActionResult EditPerson(personID)行动对我来说这样做吗? 它显示在调试器,它正在传递其适当的真正的整数通过视图,可以显示我错误的信息? – 2010-05-10 07:58:06

相关问题