我的创建操作是否需要自定义模型绑定器?

发布于 2024-11-07 05:52:29 字数 2274 浏览 3 评论 0原文

我一直在对此进行一些研究,但是,我在理解 MVC 3 中何时需要模型绑定时遇到了一些困难。我创建了一个 ViewModel 来向我的 Create 视图提供数据。

public class InvitationRequestViewModel
{
    public InvitationRequest InvitationRequest { get; private set; }

    public IEnumerable<SelectListItem> EventsList { get; private set; }

    public string EventId { get; set; }

    public InvitationRequestViewModel(InvitationRequest request)
    {
        InvitationRequest = request;
        EventsList = new SelectList(MyRepositoryAndFactory.Instance.FindAllEvents()
                .Select(events => new SelectListItem 
                { 
                    Value = events.ID.ToString(),
                    Text = String.Format("{0} - {1} - {2}", events.Name, events.Location, events.StartDate.ToShortDateString())
                }
            ), "Value", "Text");
    }
}

我的 InvitationRequest 控制器具有以下 Action 方法,

public ActionResult Create()
    {
        InvitationRequest request = new InvitationRequest(User.Identity.Name);

        return View(new InvitationRequestViewModel(request));
    } 

    [HttpPost]
    public ActionResult Create(InvitationRequestViewModel newInvitationRequest)
    {
        try
        {
            if (!ModelState.IsValid) return View(newInvitationRequest);

            newInvitationRequest.InvitationRequest.Save();
            MyRepositoryAndFactory.Instance.CommitTransaction();
            return RedirectToAction("Index");
        }
        catch
        {
            ModelState.AddModelError("","Invitation Request could not be created");
        }

        return View(newInvitationRequest);
    }

我可以毫无问题地访问 Create 视图,并且 DDL 中填充了可用事件的列表。我的问题是我期望 InvitationRequestViewModel 映射到 HttpPost Create 方法。相反,我只是收到一条错误消息“该网站无法显示该页面”。 当我使用签名时:

public ActionResult Create(FormCollection collection){ }

然后我可以看到发布的值。我希望不必在控制器中执行自己的映射代码。
自定义 ModelBinder 是答案吗?

编辑 我正在使用 InvitationRequestViewModel 类型的强类型视图,这是 DDL 代码

<div class="editor-label">
        @Html.LabelFor(model => model.InvitationRequest.Event)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(x => x.EventId, Model.EventsList)
    </div>

I have been doing a bit of research on this but, I am having a little trouble understanding when modelbinding is needed in MVC 3. I have created a ViewModel to supply data to my Create view.

public class InvitationRequestViewModel
{
    public InvitationRequest InvitationRequest { get; private set; }

    public IEnumerable<SelectListItem> EventsList { get; private set; }

    public string EventId { get; set; }

    public InvitationRequestViewModel(InvitationRequest request)
    {
        InvitationRequest = request;
        EventsList = new SelectList(MyRepositoryAndFactory.Instance.FindAllEvents()
                .Select(events => new SelectListItem 
                { 
                    Value = events.ID.ToString(),
                    Text = String.Format("{0} - {1} - {2}", events.Name, events.Location, events.StartDate.ToShortDateString())
                }
            ), "Value", "Text");
    }
}

My InvitationRequest controller has the following Action methods

public ActionResult Create()
    {
        InvitationRequest request = new InvitationRequest(User.Identity.Name);

        return View(new InvitationRequestViewModel(request));
    } 

    [HttpPost]
    public ActionResult Create(InvitationRequestViewModel newInvitationRequest)
    {
        try
        {
            if (!ModelState.IsValid) return View(newInvitationRequest);

            newInvitationRequest.InvitationRequest.Save();
            MyRepositoryAndFactory.Instance.CommitTransaction();
            return RedirectToAction("Index");
        }
        catch
        {
            ModelState.AddModelError("","Invitation Request could not be created");
        }

        return View(newInvitationRequest);
    }

I can reach the Create view with no problems and the DDL is populated with a list of available events. My problem is that I was expecting the InvitationRequestViewModel to be mapped to the HttpPost Create method. Instead, I just get an error saying "The website cannot display the page".
When I use the signature:

public ActionResult Create(FormCollection collection){ }

then I can see the posted values. I had hoped not to have to do my own mapping code in the controller.
Is a custom ModelBinder the answer?

EDIT
I am using a strongly typed View of type InvitationRequestViewModel and this is the DDL code

<div class="editor-label">
        @Html.LabelFor(model => model.InvitationRequest.Event)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(x => x.EventId, Model.EventsList)
    </div>

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

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

发布评论

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

评论(1

停顿的约定 2024-11-14 05:52:29

您必须为 InvitationRequestViewModel 指定一个无参数构造函数,以便默认模型绑定器可以实例化它。

You have to specify a parameterless constructor for the InvitationRequestViewModel so the default model binder can instantiate it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文