当我发布时,使用 SelectList 中的值填充模型上的属性

发布于 2024-09-10 11:45:13 字数 1485 浏览 7 评论 0原文

我正在构建一个 Asp.net MVC 2 应用程序。

我有一个名为 Team 的实体,它通过公共属性映射到另外两个名为 Gender 和 Grade 的实体。

public class Team
{
    public virtual int Id { get; private set; } 
    public virtual string CoachesName { get; set; } 
    public virtual string PrimaryPhone { get; set; } 
    public virtual string SecondaryPhone { get; set; }
    public virtual string EmailAddress { get; set; } 
    public virtual Grade Grade { get; set; } 
    public virtual  Gender Gender { get; set; } 
}

我有一个看起来像这样的 ViewModel。

public class TeamFormViewModel
{

    public TeamFormViewModel()
    {
        Team = new Team();
        Grade = new SelectList((new Repository<Grade>()).GetList(),"ID", "Name",Team.Grade);
        Gender = new SelectList((new Repository<Gender>()).GetList(), "ID", "Name", Team.Gender);
    }

    public Team Team { get; set; }
    public virtual SelectList Grade { get; set; }
    public virtual SelectList Gender { get; set; }
}

我的表单按照我的预期呈现。当我调试 Create 方法时,我发现我的 Team 对象上的 Gender 和 Grade 属性为 NULL。

    [HttpPost, Authorize]
    public ActionResult Create(Team team)
    {
        try
        {
            if (ModelState.IsValid)
            {
                (new Repository<Team>()).Save(team);

            }
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

我做错了什么?

谢谢, 埃里克

I'm building an Asp.net MVC 2 application.

I have an entity called Team that is mapped via public properties to two other entities called Gender and Grade.

public class Team
{
    public virtual int Id { get; private set; } 
    public virtual string CoachesName { get; set; } 
    public virtual string PrimaryPhone { get; set; } 
    public virtual string SecondaryPhone { get; set; }
    public virtual string EmailAddress { get; set; } 
    public virtual Grade Grade { get; set; } 
    public virtual  Gender Gender { get; set; } 
}

I have a ViewModel that looks like this.

public class TeamFormViewModel
{

    public TeamFormViewModel()
    {
        Team = new Team();
        Grade = new SelectList((new Repository<Grade>()).GetList(),"ID", "Name",Team.Grade);
        Gender = new SelectList((new Repository<Gender>()).GetList(), "ID", "Name", Team.Gender);
    }

    public Team Team { get; set; }
    public virtual SelectList Grade { get; set; }
    public virtual SelectList Gender { get; set; }
}

My form renders as I would expect. When I debug the Create method I see that the Gender and Grade properties are NULL on my Team object.

    [HttpPost, Authorize]
    public ActionResult Create(Team team)
    {
        try
        {
            if (ModelState.IsValid)
            {
                (new Repository<Team>()).Save(team);

            }
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

What am I doing wrong?

Thanks,
Eric

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

郁金香雨 2024-09-17 11:45:13

我建议您发布并绑定回视图模型类而不是实体类。为您的视图模型类创建一个扩展方法,该方法将返回您的实体类。这是一些工作代码:

public class Team
{
   public virtual int Id { get; set; }
   public virtual string CoachesName { get; set; }
   public virtual string PrimaryPhone { get; set; }
   public virtual string SecondaryPhone { get; set; }
   public virtual string EmailAddress { get; set; }
   public virtual Grade Grade { get; set; }
   public virtual Gender Gender { get; set; }
}

public class Grade
{
   public virtual int Id { get; set; }
   public virtual string Name { get; set; }
}

public class Gender
{
   public virtual int Id { get; set; }
   public virtual string Name { get; set; }
}

public class TeamFormViewModel
{
   public TeamFormViewModel()
   {
      var gradeList = (new Repository<Grade>()).GetList();
      var genderList = (new Repository<Gender>()).GetList();
      GradeList = new SelectList(gradeList, "Id", "Name");
      GenderList = new SelectList(genderList, "Id", "Name");
   }

   [HiddenInput(DisplayValue = false)]
   public int Id { get; set; }

   [DisplayName("Coach Name")]
   [Required]
   public string CoachesName { get; set; }

   [DisplayName("Primary Phone")]
   [DataType(DataType.PhoneNumber)]
   [Required]
   public string PrimaryPhone { get; set; }

   [DisplayName("Secondary Phone")]
   [DataType(DataType.PhoneNumber)]
   public string SecondaryPhone { get; set; }

   [DisplayName("Email Address")]
   [DataType(DataType.EmailAddress)]
   [Required]
   public string EmailAddress { get; set; }

   [DisplayName("Grade")]
   [Range(1, 5)]
   public int SelectedGradeId { get; set; }

   [DisplayName("Gender")]
   [Range(1, 5)]
   public int SelectedGenderId { get; set; }

   private int selectedGradeId = 0;
   private int selectedGenderId = 0;

   public SelectList GradeList { get; set; }
   public SelectList GenderList { get; set; }
}

public static class TeamExtensions
{
   public static Team ToTeam(this TeamFormViewModel viewModel)
   {
      return new Team
      {
         Id = viewModel.Id,
         CoachesName = viewModel.CoachesName,
         PrimaryPhone = viewModel.PrimaryPhone,
         SecondaryPhone = viewModel.SecondaryPhone,
         EmailAddress = viewModel.EmailAddress,
         Grade = (new Repository<Grade>())
            .GetList()
            .Where(x => x.Id == viewModel.SelectedGradeId)
            .Single(),
         Gender = (new Repository<Gender>())
            .GetList()
            .Where(x => x.Id == viewModel.SelectedGradeId)
            .Single()
      };
   }

   public static TeamFormViewModel ToTeamFormViewModel(this Team team)
   {
      return new TeamFormViewModel
      {
         Id = team.Id,
         CoachesName = team.CoachesName,
         PrimaryPhone = team.PrimaryPhone,
         SecondaryPhone = team.SecondaryPhone,
         EmailAddress = team.EmailAddress,
         SelectedGradeId = team.Grade.Id,
         SelectedGenderId = team.Gender.Id
      };
   }
}

public class TeamController : Controller
{
   public ActionResult Create()
   {
      var viewModel = new TeamFormViewModel();
      return View(viewModel);
   }

   [HttpPost]
   public ActionResult Create(TeamFormViewModel viewModel)
   {
      if (ModelState.IsValid)
      {
         (new Repository<Team>())
            .Save(viewModel.ToTeam());
      }
      return View(viewModel);
   }
}

最后是视图:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Stack1.Models.TeamFormViewModel>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title>Create</title>
   <script type="text/javascript" src="/Scripts/jquery-1.4.1.js"></script>
   <script type="text/javascript" src="/Scripts/MicrosoftAjax.js"></script>
   <script type="text/javascript" src="/Scripts/MicrosoftMvcValidation.js"></script>
</head>
<body>
   <% Html.EnableClientValidation(); %>
   <% using (Html.BeginForm()) { %>
   <%= Html.ValidationSummary() %>
   <fieldset>
      <legend>Fields</legend>
      <%= Html.LabelFor(x => x.CoachesName) %>
      <p>
         <%= Html.TextBoxFor(x => x.CoachesName) %>
         <%= Html.ValidationMessageFor(x => x.CoachesName) %>
      </p>

      <%= Html.LabelFor(x => x.PrimaryPhone)%>
      <p>
         <%= Html.EditorFor(x => x.PrimaryPhone) %>
         <%= Html.ValidationMessageFor(x => x.PrimaryPhone)%>
      </p>

      <%= Html.LabelFor(x => x.SecondaryPhone)%>
      <p>
         <%= Html.EditorFor(x => x.SecondaryPhone) %>
         <%= Html.ValidationMessageFor(x => x.SecondaryPhone)%>
      </p>

      <%= Html.LabelFor(x => x.EmailAddress)%>
      <p>
         <%= Html.EditorFor(x => x.EmailAddress) %>
         <%= Html.ValidationMessageFor(x => x.EmailAddress)%>
      </p>

      <%= Html.LabelFor(x => x.SelectedGradeId)%>
      <p>
         <%= Html.DropDownListFor(x => x.SelectedGradeId, Model.GradeList) %>
         <%= Html.ValidationMessageFor(x => x.SelectedGradeId)%>
      </p>

      <%= Html.LabelFor(x => x.SelectedGenderId)%>
      <p>
         <%= Html.DropDownListFor(x => x.SelectedGenderId, Model.GenderList) %>
         <%= Html.ValidationMessageFor(x => x.SelectedGenderId)%>
      </p>
      <p>
         <%= Html.HiddenFor(x => x.Id) %>
         <input type="submit" value="Save" />
      </p>
   </fieldset>
   <% } %>
</body>
</html>

I recommend that you post and bind back to a view model class rather than your entity class. Create an extension method for your view model class that will return your entity class. Here's some working code:

public class Team
{
   public virtual int Id { get; set; }
   public virtual string CoachesName { get; set; }
   public virtual string PrimaryPhone { get; set; }
   public virtual string SecondaryPhone { get; set; }
   public virtual string EmailAddress { get; set; }
   public virtual Grade Grade { get; set; }
   public virtual Gender Gender { get; set; }
}

public class Grade
{
   public virtual int Id { get; set; }
   public virtual string Name { get; set; }
}

public class Gender
{
   public virtual int Id { get; set; }
   public virtual string Name { get; set; }
}

public class TeamFormViewModel
{
   public TeamFormViewModel()
   {
      var gradeList = (new Repository<Grade>()).GetList();
      var genderList = (new Repository<Gender>()).GetList();
      GradeList = new SelectList(gradeList, "Id", "Name");
      GenderList = new SelectList(genderList, "Id", "Name");
   }

   [HiddenInput(DisplayValue = false)]
   public int Id { get; set; }

   [DisplayName("Coach Name")]
   [Required]
   public string CoachesName { get; set; }

   [DisplayName("Primary Phone")]
   [DataType(DataType.PhoneNumber)]
   [Required]
   public string PrimaryPhone { get; set; }

   [DisplayName("Secondary Phone")]
   [DataType(DataType.PhoneNumber)]
   public string SecondaryPhone { get; set; }

   [DisplayName("Email Address")]
   [DataType(DataType.EmailAddress)]
   [Required]
   public string EmailAddress { get; set; }

   [DisplayName("Grade")]
   [Range(1, 5)]
   public int SelectedGradeId { get; set; }

   [DisplayName("Gender")]
   [Range(1, 5)]
   public int SelectedGenderId { get; set; }

   private int selectedGradeId = 0;
   private int selectedGenderId = 0;

   public SelectList GradeList { get; set; }
   public SelectList GenderList { get; set; }
}

public static class TeamExtensions
{
   public static Team ToTeam(this TeamFormViewModel viewModel)
   {
      return new Team
      {
         Id = viewModel.Id,
         CoachesName = viewModel.CoachesName,
         PrimaryPhone = viewModel.PrimaryPhone,
         SecondaryPhone = viewModel.SecondaryPhone,
         EmailAddress = viewModel.EmailAddress,
         Grade = (new Repository<Grade>())
            .GetList()
            .Where(x => x.Id == viewModel.SelectedGradeId)
            .Single(),
         Gender = (new Repository<Gender>())
            .GetList()
            .Where(x => x.Id == viewModel.SelectedGradeId)
            .Single()
      };
   }

   public static TeamFormViewModel ToTeamFormViewModel(this Team team)
   {
      return new TeamFormViewModel
      {
         Id = team.Id,
         CoachesName = team.CoachesName,
         PrimaryPhone = team.PrimaryPhone,
         SecondaryPhone = team.SecondaryPhone,
         EmailAddress = team.EmailAddress,
         SelectedGradeId = team.Grade.Id,
         SelectedGenderId = team.Gender.Id
      };
   }
}

public class TeamController : Controller
{
   public ActionResult Create()
   {
      var viewModel = new TeamFormViewModel();
      return View(viewModel);
   }

   [HttpPost]
   public ActionResult Create(TeamFormViewModel viewModel)
   {
      if (ModelState.IsValid)
      {
         (new Repository<Team>())
            .Save(viewModel.ToTeam());
      }
      return View(viewModel);
   }
}

And finally, the view:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Stack1.Models.TeamFormViewModel>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title>Create</title>
   <script type="text/javascript" src="/Scripts/jquery-1.4.1.js"></script>
   <script type="text/javascript" src="/Scripts/MicrosoftAjax.js"></script>
   <script type="text/javascript" src="/Scripts/MicrosoftMvcValidation.js"></script>
</head>
<body>
   <% Html.EnableClientValidation(); %>
   <% using (Html.BeginForm()) { %>
   <%= Html.ValidationSummary() %>
   <fieldset>
      <legend>Fields</legend>
      <%= Html.LabelFor(x => x.CoachesName) %>
      <p>
         <%= Html.TextBoxFor(x => x.CoachesName) %>
         <%= Html.ValidationMessageFor(x => x.CoachesName) %>
      </p>

      <%= Html.LabelFor(x => x.PrimaryPhone)%>
      <p>
         <%= Html.EditorFor(x => x.PrimaryPhone) %>
         <%= Html.ValidationMessageFor(x => x.PrimaryPhone)%>
      </p>

      <%= Html.LabelFor(x => x.SecondaryPhone)%>
      <p>
         <%= Html.EditorFor(x => x.SecondaryPhone) %>
         <%= Html.ValidationMessageFor(x => x.SecondaryPhone)%>
      </p>

      <%= Html.LabelFor(x => x.EmailAddress)%>
      <p>
         <%= Html.EditorFor(x => x.EmailAddress) %>
         <%= Html.ValidationMessageFor(x => x.EmailAddress)%>
      </p>

      <%= Html.LabelFor(x => x.SelectedGradeId)%>
      <p>
         <%= Html.DropDownListFor(x => x.SelectedGradeId, Model.GradeList) %>
         <%= Html.ValidationMessageFor(x => x.SelectedGradeId)%>
      </p>

      <%= Html.LabelFor(x => x.SelectedGenderId)%>
      <p>
         <%= Html.DropDownListFor(x => x.SelectedGenderId, Model.GenderList) %>
         <%= Html.ValidationMessageFor(x => x.SelectedGenderId)%>
      </p>
      <p>
         <%= Html.HiddenFor(x => x.Id) %>
         <input type="submit" value="Save" />
      </p>
   </fieldset>
   <% } %>
</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文