0

我正在学习如何使用 ViewModels 将信息从视图传递到控制器,反之亦然。我的创建操作、视图和视图模型工作正常,但我在编辑时遇到了问题。我得到错误:

传入字典的模型项的类型为“CatVM.Models.Cat”,但此字典需要类型为“CatVM.Models.EditCatViewModel”的模型项。

这是我的代码:

控制器方法

    // GET: /Cats/Edit/5
    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Cat cat = unitOfWork.CatRepository.GetByID(id);
        if (cat == null)
        {
            return HttpNotFound();
        }
        return View(cat);
    }

看法

@model CatVM.Models.EditCatViewModel

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>


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

<div class="form-horizontal">
    <h4>Cat</h4>
    <hr />
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.ID)

    <div class="form-group">
        @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Color, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Color)
            @Html.ValidationMessageFor(model => model.Color)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.FurLength, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.FurLength)
            @Html.ValidationMessageFor(model => model.FurLength)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Size, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Size)
            @Html.ValidationMessageFor(model => model.Size)
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

编辑猫视图模型

public class EditCatViewModel
{
    public int ID { get; set; }

    [Required]
    [StringLength(50)]
    public string Name { get; set; }

    [Required]
    [StringLength(50)]
    public string Color { get; set; }

    [Required]
    [StringLength(50)]
    [Display(Name = "Fur Type")]
    public string FurLength { get; set; }

    [StringLength(50)]
    public string Size { get; set; }
}
}
4

2 回答 2

3

那是因为您收到的项目CatRepository.GetByID(id);是 type Cat,而不是EditCatViewModel

您可以通过从此对象构造一个新的视图模型来绕过它:

Cat cat = unitOfWork.CatRepository.GetByID(id);
var viewModel = new EditCatViewModel {
 Name = cat.Name,
 Color = cat.Color,
 FurLength = cat.FurLength,
 Size = cat.Size
};

return View(viewModel);

或者,您可以构造隐式显式转换方法或使用像AutoMapper这样的映射工具。

于 2013-11-03T15:14:11.863 回答
1

您的返回视图应该是 CatVM.Models.EditCatViewModel 类型,现在您返回的是 Cat

返回视图(猫);

在视图模型中转换您的模型并将此对象传递回视图

于 2013-11-03T15:13:40.380 回答