asp.net mvc中的局部视图问题 [英] Partial view problem in asp.net mvc

查看:121
本文介绍了asp.net mvc中的局部视图问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个局部视图,并在我的项目中的Create.chtml中添加它,它将在创建页面中显示数据库中的所有区域。一切(插入,更新,删除)工作正常,但当我点击创建按钮而不从下​​拉列表中选择一个项目时,它会生成错误。



I create a partial view and add this in "Create.chtml" in my project which will display all area from database in create page. Everything(Insert, Update,Delete) works fine but when i click on the create button without select an item from dropdownlist it generate an error.

The model item passed into the dictionary is of type 'NHPractice2.Models.Area', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[NHPractice2.Models.Area]'.





部分视图



Partial View

@model IEnumerable<NHPractice2.Models.Area>
@using NHPractice2.Models

@{
    WebGrid grdArea = new WebGrid(Model, canPage: true, rowsPerPage: 10, defaultSort: "Name");
}

@grdArea.GetHtml(tableStyle: "Grid", headerStyle: "Header", alternatingRowStyle: "altRow", columns: grdArea.Columns(
    grdArea.Column("Sl No.", format: item => item.WebGrid.Rows.IndexOf(item) + 1, style: "serialCol"),
    grdArea.Column("Area", format: item => item.Name),
    grdArea.Column("City", format: item => item.CityName),
    grdArea.Column("Action", format: @<text>@Html.ActionLink("Edit", "Edit", new { id = item.ID }, new { onclick = "return confirm('Are sure wants to edit?');" }) | @Html.ActionLink("Delete", "Delete", new { id = item.ID }, new { onclick = "return confirm('Are sure wants to delete?');" })</text>)

        )
           )





控制器代码



Controller Code

[ChildActionOnly]
public ActionResult AreaList()
{            
  IEnumerable<Area> areas = AreaRepo.ShowAreaList(1, 0, "", 0);
  return PartialView("AreaList",areas);
}





来自Create.chtml的代码



Code from Create.chtml

@Html.Action("AreaList", new List<NHPractice2.Models.Area> { new NHPractice2.Models.Area() });





我尝试了什么:



我多次尝试解决但结果相同。



What I have tried:

I tried several times to solve it but same result.

推荐答案

由于模型局部视图和将模型发送到部分视图而发生错误。尝试列表如 @model List< NHPractice2.Models.Area> @model IEnumerable< NHPractice2.Models.Area> 。尝试以下步骤:



在PartialView

Error occurs due to model partial view and sending model to partial view.Try List like @model List<NHPractice2.Models.Area> instead of @model IEnumerable<NHPractice2.Models.Area>. Try with below steps:

In PartialView
@model List<NHPractice2.Models.Area>
@using NHPractice2.Models
 
@{
    WebGrid grdArea = new WebGrid(Model, canPage: true, rowsPerPage: 10, defaultSort: "Name");
}



在控制器中


In Controller

[ChildActionOnly]
public ActionResult AreaList()
{            
  List<Area> areas = (AreaRepo.ShowAreaList(1, 0, "", 0)).ToList();
  return PartialView("AreaList", areas);
}



在视图中(调用部分视图)


In view(to call partial view)

@Html.Action("AreaList", "ControllerName");


这篇关于asp.net mvc中的局部视图问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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