如何将MVC POST名称/值绑定到控制器ViewModel

时间:2014-09-26 09:56:04

标签: c# asp.net-mvc http

我试图将HTML表单发布到我的PersonController.AppSave(),但我认为映射/绑定存在问题。

我通过POST { Person.id: '12345, XApplication.dateOfApplication: '01/01/2014'}发送两个值,但是我的控制器似乎没有直接识别这些值,因为它期待XApplication(有充分的理由)。

如何绑定这些值?例如,我想将Person.id绑定到XApplication.personId

我做了一些根本错误的事情吗?

感谢您的帮助

@model MyNameSpace.ViewModels.StudentViewModel

    @using (Html.BeginForm("Save", "Person", FormMethod.Post))
    {
    <div class="row">
        <div class="col-md-12">
            @Html.HiddenFor(m => m.Person.id, "default")
            <div class="form-group">
                <label for="dateOfApplication" class="col-md-3 control-label">Date of Application</label>
                <div class="col-md-9">
                    @Html.EditorFor(m => m.XApplication.dateOfApplication, new { htmlAttributes = new { @class = "form-control datepicker" } })
                </div>
            </div><!-- form-group -->
        </div><!-- .12 -->
    </div><!-- .row -->
    }


namespace MyNameSpace.ViewModels
{
      public class PersonViewModel
      {
        public Person Person { get; set; }
        public XApplication XApplications { get; set; }
      }
  }

namespace MyNameSpace.Controllers
{
    public class PersonController : Controller
    {
      [HttpPost]
      [Route("person/appSave")]
      public ActionResult AppSave(XApplication application)
      {
        return Redirect("/person/app/" + application.personId);
      }
    }
}

namespace MyNameSpace.Models
{
    public partial class XApplication
    {
        public int id { get; set; }

        [Required]
        public int personId { get; set; }

        [Required]
        [Column(TypeName = "date")]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime dateOfApplication { get; set; }
    }
}

0 个答案:

没有答案