【问题标题】:How to return two models in MVC?如何在 MVC 中返回两个模型?
【发布时间】:2015-03-11 11:57:33
【问题描述】:

我有这个将在控制器中调用的函数:

public EditViewModel PostEditViewModel(EditViewModel model)
    {            
        using (var db = new NorthwindEntities())
        {
            var prod = db.Products.Where(x => x.Id == model.Id).Single();
            {
                prod.Id = model.Id;
                ...
                //I need something like this:
                //prod.CategoryID = model.CategoryList.CatId
                //but obviously intellisense tells me that after the dot of CategoryList, only methods of that list can be called.
                db.SaveChanges();
            }

这是我的 ViewModel:

    public int Id{ get; set; }
    ...
    public IEnumerable<Categories> CategoryList { get; set; }

    public class Categories {
    public int ProdId { get; set; }
    public int? CatId { get; set; }
    public string CatName { get; set; }
    }

如何通过我的 EditViewModel 调用 CategoryList,以便我可以通过 HTML.DropdownList 编辑特定产品的 Category?

【问题讨论】:

  • 您可以在视图中发布您的 DropDownList 的剃刀/标记吗?
  • @Html.DropDownList("CategoriesDropdown", Model.CategoryName)

标签: c# asp.net-mvc entity-framework model-view-controller


【解决方案1】:

如果您的产品模型具有CategoryId 属性(我只是在您的问题中看不到它)并且您使用强类型视图您始终可以使用DropDownListBoxFor() 助手的重载:

@Html.DropDownListFor(
    x => x.CategoryId,
    new SelectList(Model.CategoryList, "CatId", "CatName")
)

但实际上我建议您在所有下拉列表中使用ViewModels 中的SelectListItem,因为这是非常糟糕的做法 - 将域实体放在您的 View

你的ViewModel 会是这样的:

public int Id { get; set; }
public int CategoryId { get; set; } 
...
public IEnumerable<SelectListItem> CategoryList { get; set; }

在视图上你可以这样做:

@Html.DropDownListFor(x => x.CategoryId, Model.CategoryList)

在您的 GET ViewModel Controller 中,您可以像这样初始化您的 CategoryList

        model.CategoryList = db.Categories.OrderBy(x => x.Name)
            .Select(x => new SelectListItem
            {
                Text = x.Name,
                Value = x.Id.ToString()
            });

它确实可以帮助您使视图更清晰。

【讨论】:

  • 它表示该值不能为空,但感谢 Teo 的洞察力
  • 初始化你的选择列表:) selectlistitem 列表,它会worr
  • 对不起,Teo,正如你所想的那样,我真的是 MVC 新手。正如你所建议的,我使用了 SelectListItem,但现在,删除了 IEnumerable CategoryList { get;放; } 部分,我无法将模型转换为列表:
  • model.CategoryList = (from c in db.Categories orderby c.Name select new EditViewModel { CategoryId = c.Id, CategoryName = c.Name }).ToList();
  • 给你,我更新了答案 =) 你如何初始化你的 CategoryList
【解决方案2】:

看起来CategoryList 用于填充 DropDownList 中的项目,CatId 是视图模型上捕获所选类别 ID 值的属性。

如果是这样,你可以像这样分配它:

if (model.CatId.HasValue) 
{
  prod.CategoryID = model.CatId.Value;
}

【讨论】:

  • 问题是,有一个 EditViewModel 模型,我不能调用 CategoryList。它不调用 CatId,而是调用 CategoryList。像这样: if (model.CategoryList
  • 对,我重新阅读了您的原始帖子,发现 CatId 只是 Categories 类的一个道具。您需要向视图模型添加一个道具。如果您确保在渲染视图之前初始化 CategoryList,teo van kot 建议的方法应该可以工作。
【解决方案3】:

如果我没记错,我理解你,你应该在这个类中创建类EditViewModel创建字段:

public int Id{ get; set; }
...
public IEnumerable<Categories> CategoryList { get; set; }

接下来,在您的控制器中,您应该使用以下代码:

var prod = db.Products.Where(x => x.Id == model.Id).Single();
            {
                prod.Id = model.Id;
                ...
                prod.CategoryID = model.CategoryList.Select(m => m.CatId)
                //but Select returned the List of CatId, I suggest thet prod.CategoryID is List               
            }
db.SaveChanges();

【讨论】:

  • 它说它不能将 IEnumerable 隐式转换为 'int?'
  • @JennaO'Connor 是的,当然,因为 Select 方法返回 IEnumerable 类型。我在评论部分的写法,我建议 prod.CategoryID 是 List,或者你可以使用prod.CategoryID = model.Category List.Select(m =&gt; m.CatId).FirstOrDefault()
  • 谢谢!但是现在,它说:值不能为空,参数名称:源并指向您建议我的行:prod.Category.Id = model.CategoryList.Select(m => m.CatId).FirstOrDefault();
  • @JennaO'Connor 检查 CategoryList 是否为空
猜你喜欢
  • 1970-01-01
  • 2022-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多