ViewModel不会填充输入字段

时间:2014-03-12 14:28:05

标签: asp.net-mvc mvvm

我有一个用于编辑视图的ViewModel。返回数据时,编辑视图中不会显示要编辑的项目的ID。

public ActionResult Edit(int id)
{
    Parcel parcel = _parcelDao.GetParcel(id);

    ParcelEditViewModel viewModel = new ParcelEditViewModel();

    viewModel.id = parcel.id;
    return View(viewModel);
}

我应该返回与View不同的内容吗?

视图模型

public class ParcelEditViewModel
{

    public Parcel parcel { get; set; }
    public int id { get; set; }
    public IEnumerable<SelectListItem> TrackStatus { get; set; }

    public ParcelEditViewModel()
    {
        parcel = new Parcel();

        TrackStatus = new List<SelectListItem>
        {
            new SelectListItem
            {
                Value = "AwaitingCollection",
                Text = "Awaiting Collection"
            },
            new SelectListItem
            {
                Value = "OutForDelivery",
                Text = "Out For Delivery"
            },
            new SelectListItem
            {
                Value = "Delivered",
                Text = "Delivered"
            }

    };

    }
    public int ParcelStatus { get; set; }
}

查看:

//@model ABC.Data.Parcel
@model Abc.ViewModels.ParcelEditViewModel

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Parcel</legend>

    @Html.HiddenFor(model => model.parcel.id)

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

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

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

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

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

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

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

    <div class="editor-label">
        @Html.LabelFor(model => model.ParcelStatus)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.parcel.TrackingStatus, Model.TrackStatus)
        @Html.ValidationMessageFor(model => model.ParcelStatus)
    </div>

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

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

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

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

1 个答案:

答案 0 :(得分:0)

您需要填充视图模型属性的所有;不只是身份证。 ParcelEditViewModel不知道您的数据库中有什么。

public ActionResult Edit(int id)
{
    Parcel parcel = _parcelDao.GetParcel(id);
    ParcelEditViewModel viewModel = new ParcelEditViewModel();
    viewModel.id = parcel.id;

    // Populate other properties! These may not be right.
    viewModel.ProductName = parcel.Title;
    viewModel.RecipientName; = parcel.RecipientName;

    return View(viewModel);
}

有一些映射库,例如AutoMapper,可以帮助简化这一点。