首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >复合ViewModel和UpdateModel

复合ViewModel和UpdateModel
EN

Stack Overflow用户
提问于 2011-04-16 02:25:13
回答 1查看 1.8K关注 0票数 2

在POST操作中,在未填充的值中缺少什么?

控制器

代码语言:javascript
复制
public ActionResult Index()
{
    var productPageViewModel = new ProductPageViewModel();

    productPageViewModel.ProductPageCriteria = BuildProductPageCriteriaViewModel();
    productPageViewModel.Products = GetProducts(productPageViewModel.ProductPageCriteria);

    return View(productPageViewModel);
}

[HttpPost]
public ActionResult Index(ProductPageViewModel productPageViewModel, FormCollection formCollection)
{
    // productPageViewModel is not populated with posted values of ProductPageCriteria.CategoryID, ProductPageCriteria.DepartmentID and ProductPageCriteria.PageSize
    // formCollection has correct values
    // Calling UpdateModel(productPageViewModel); has no affect - makes sense, the framework has already called it
    // Calling UpdateModel(productPageViewModel.ProductPageCriteria); populates the values.
    // The renderd form has names like CategoryID, DepartmentID unlike ProductPageCriteria.CategoryID, ProductPageCriteria.DepartmentID
    //     if the top model was passed to all partial views also.

    return View(productPageViewModel);
}

模型

代码语言:javascript
复制
public class ProductPageCriteriaViewModel
{
    public const int DefaultPageSize = 15;

    public ProductPageCriteriaViewModel()
    {
        Categories = new List<Category>();
        Departments = new List<Department>();

        PageSize = DefaultPageSize;
    }

    [Display(Name = "Category")]
    public int? CategoryID { get; set; }
    [Display(Name = "Department")]
    public int DepartmentID { get; set; }
    [Display(Name = "Page Size")]
    public int? PageSize { get; set; }

    public List<Category> Categories { get; set; }
    public List<Department> Departments { get; set; }
}

public class ProductPageViewModel
{
    public ProductPageViewModel()
    {
        ProductPageCriteria = new ProductPageCriteriaViewModel();
        Products = new List<Product>();
    }

    public ProductPageCriteriaViewModel ProductPageCriteria { get; set; }
    public List<Product> Products { get; set; }
}

public class Product
{
    public int ProductID { get; set; }
    public string ProductName { get; set; }

    public Category Category { get; set; }
    public Department Department { get; set; }
}

public class Category
{
    public int CategoryID { get; set; }
    public string CategoryName { get; set; }
}

public class Department
{
    public int DepartmentID { get; set; }
    public string DepartmentName { get; set; }
}

查看Index.cshtml

代码语言:javascript
复制
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    @Html.Partial("_ProductCriteria", Model.ProductPageCriteria)

    @Html.Partial("_ProductList", Model.Products)
}

部分视图_ProductCriteria.cshtml

代码语言:javascript
复制
@model Mvc3Application4.Models.ProductPageCriteriaViewModel

<fieldset>
    <legend>Criteria</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.CategoryID)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.CategoryID, new SelectList(Model.Categories, "CategoryID", "CategoryName", Model.CategoryID), "--- All ---")
        @Html.ValidationMessageFor(model => model.CategoryID)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.DepartmentID)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.DepartmentID, new SelectList(Model.Departments, "DepartmentID", "DepartmentName", Model.DepartmentID), "--- All ---")
        @Html.ValidationMessageFor(model => model.DepartmentID)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.PageSize)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.PageSize, new SelectList(new List<int> {10, 15, 20, 25, 50, 100}.Select(n => new {Value = n, Text = n}), "Value", "Text", Model.PageSize), "--- All ---")
        @Html.ValidationMessageFor(model => model.PageSize)
    </div>

    <p>
        <input type="submit" value="Search" />
    </p>
</fieldset>

部分视图_ProductList.cshtml

代码语言:javascript
复制
@model IEnumerable<Mvc3Application4.Models.Product>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th></th>
        <th>
            ProductName
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ProductID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ProductID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ProductID })
        </td>
        <td>
            @item.ProductName
        </td>
    </tr>
}

</table>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-16 10:54:12

这是我没有想到的,也是未经测试的,但是我相信,如果您将父模型(ProductPageViewModel)传递给products criteria部分视图,更改部分视图以继承此模型,并将控件更改为从model => model.ProductPageCriteria.CategoryID使用而不是从model => model.CategoryID使用,它应该保持命名,以便UpdateModel可以将字段与发布的值相匹配。

抱歉,如果这句话是错误的,我相信我很快就能拿到我的同龄人压力徽章。:)希望这能有所帮助。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5680829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档