ASP.NET. MVC2. Entity Framework. Невозможно передать значение первичного ключа из представления в [HttpPost] - PullRequest
0 голосов
/ 10 мая 2010

Я передаю ViewModel (который содержит объект "Person") из действия контроллера "EditPerson" в представление. При отправке обратно из представления ActionResult получает все свойства Person, кроме идентификатора (который, как он говорит, равен нулю, а не как его действительное целое число)

Может кто-нибудь сказать мне, почему?

Контроллеры выглядят так:

        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; }
}

И ViewModel:

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; }
}

}

Ответы [ 2 ]

1 голос
/ 10 мая 2010

Удалить

[Bind(Exclude = "ID")] 

от вашего модельного класса.

1 голос
/ 10 мая 2010

Идентификатор не опубликован, потому что он не находится внутри формы. Вы можете включить его в качестве скрытого поля:

<%= Html.HiddenFor(model => model.Person.ID) %>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...