如何将ViewModel传递给ASP.NET MVC4中的[HttpPost]编辑操作 [英] How do I pass a ViewModel to an [HttpPost] Edit Action in ASP.NET MVC4

查看:69
本文介绍了如何将ViewModel传递给ASP.NET MVC4中的[HttpPost]编辑操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为Edit方法创建了HTTPGet,但是我遇到了如何创建HTTPPost的问题。它总是返回null和错误System.InvalidOperationException:Sequence不包含任何元素。任何帮助。



I have created the HTTPGet for the Edit method, but i have problems how to create the HTTPPost. it always returns null and error System.InvalidOperationException: Sequence contains no elements. Any help.

[HttpGet]
public ActionResult Edit(int? entryId)
{
   var customerViewModel = new EditEntryViewModel();

    if (entryId.HasValue)
    {
        Entry customer = _db.Entries.SingleOrDefault(x => x.Id == entryId);

        if (customer != null)
        {
            customerViewModel.Title = customer.Title;
            customerViewModel.Username = customer.Username;
            customerViewModel.Password = customer.Password;
            customerViewModel.Url = customer.Url;
            customerViewModel.Description = customer.Description;
        }
    }

    return View(customerViewModel);
}

[HttpPost]
public ActionResult Edit(EditEntryViewModel viewModel)
{
    if (ModelState.IsValid)
    {
        var entryToUpdate = _db.Entries.Single(p => p.Id == viewModel.Id);
        var entry = new Entry();

        entry.Title = viewModel.Title;
        entry.Username = viewModel.Username;
        entry.Password = viewModel.Password;
        entry.Url = viewModel.Url;
        entry.Description = viewModel.Description;



        _db.Save();

        return RedirectToAction("Detail", "Department", new { id = viewModel.Id });
    }

    return View(viewModel);
}





和视图:



And the View:

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>EditEntryViewModel</legend>

        @Html.HiddenFor(model => model.Id)

        <div class="editor-label">
            @Html.LabelFor(model => model.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Title)
            @Html.ValidationMessageFor(model => model.Title)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Username)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Username)
            @Html.ValidationMessageFor(model => model.Username)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Password)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Password)
            @Html.ValidationMessageFor(model => model.Password)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Url)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Url)
            @Html.ValidationMessageFor(model => model.Url)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Description)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Description)
            @Html.ValidationMessageFor(model => model.Description)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

推荐答案

显示您的查看代码。

最重要的代码
show your View code.
most important code


这篇关于如何将ViewModel传递给ASP.NET MVC4中的[HttpPost]编辑操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆