我的[建立"查看我的"指数"看来,这可能吗? [英] My "Create" View in my "Index" View, is it possible?
问题描述
我'使用ASP.NET MVC 4 EF,我与索引的PostController的和创建视图。我想同时拥有在同一页面上添加一个帖子,和可视化它在同一页上。我该怎么办呢?
I'am using ASP.NET MVC 4 with EF, I have a PostController with the Index and Create views. I would like to have both on the same page for adding a post, and visualize it on the same page. How can I do it ?
感谢您的意见
的___ 修改的 __
控制器:
public ActionResult Index()
{
return View(db.Posts.ToList());
}
[HttpGet]
public ActionResult Create()
{
return PartialView("_Create", **Here I can't declare model**);
}
[HttpPost]
public ActionResult Create(FormCollection values)
{
var post = new Post();
TryUpdateModel(post);
if (ModelState.IsValid)
{
/** somme code **/
db.Posts.Add(post);
db.SaveChanges();
return RedirectToAction("Index");
}
return View("_Create", post);
}
我_create局部视图:
My _Create partial view :
@model MyProject.Models.Post
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
/**some stuff **/
}
我的索引视图:
@model IEnumerable<MyProject.Models.Post>
@{
ViewBag.Title = "Index";
}
<p>
/** some stuff **/
</p>
@Html.Partial("_Create", **Here I can't declare model**)
和我的帖子型号:
public int PostId { get; set; }
public int UserId { get; set; }
public string Content { get; set; }
它告诉我,传递到字典的模型项的类型是System.Collections.Generic.List`1 [MyProject.Models.Post]',但本词典需要类型'MyProject.Models的典范项目。邮报。
It tells me that "The model item passed into the dictionary is of type ‘System.Collections.Generic.List`1[MyProject.Models.Post]’ but this dictionary requires a model item of type ‘MyProject.Models.Post‘.
推荐答案
这真的取决于你的code的外观现在。如果你有一个单独的创建
操作方法返回一个 PartialView
,就像这样:
It really depends on how your code looks right now. If you have a separate Create
action method returning a PartialView
, like so:
[HttpGet]
public ActionResult Create()
{
// Do stuff here to populate your model, if necessary
return PartialView("_Create", model);
}
然后在你的观点,你会使用 Html.RenderAction()
您希望 _create
局部视图为显示:
Then in your view you would use Html.RenderAction()
where you want the _Create
partial view to be displayed:
<div id="IndexViewStuff">
<p>Some stuff in your normal Index view.</p>
@{Html.RenderAction("Create", "Post");}
</div>
如果你没有为单独的操作方法创建
,只是有一个局部视图,使事情更清洁,那么简单的使用 HTML。部分()在
:首页
视图会做的伎俩
If you don't have a separate action method for Create
and just have a partial view to make things cleaner, then simply using Html.Partial()
in your Index
view will do the trick:
@Html.Partial("_Create") // If your _Create partial does not require a model
@Html.Partial("_Create", Model.CreateViewModel) // If it does require a model
更新
通过您的code看后,有两种方法可以做到这一点(我会告诉你两者)。出现您的问题,因为,您将帖子送到你的首页
查看列表,而你的 _create
局部视图需要一个单一的发表
模式。既然你没有明确传递模型局部视图,当你调用它,它会自动尝试使用在首页
视图的模型(你的帖子列表) 。解决这个问题的第一种方法需要很少的更改您的code。
After looking through your code, there are two ways you can do it (I'll show you both). Your problem occurs because you're passing a list of posts to your Index
view while your _Create
partial view requires a single Post
model. Since you aren't explicitly passing a model to the partial view when you're calling it, it automatically tries to use the model in the Index
view (your list of posts). The first way to solve the problem requires minimal changes to your code.
更改创建
操作方法如下:
[HttpGet]
public ActionResult Create()
{
// You have to pass a new Post
return PartialView("_Create", new MyProject.Models.Post());
}
然后在你的首页
视图,使用:
@{Html.RenderAction("Create", "Post");}
第二种方法是使用一个公开帖子列表中显示的首页
视图视图模型,也有一个空 _create
局部视图一个新的职位>发表模式。我preFER这种方法,但它是你的电话。
The second method is to use a view model that exposes the list of posts to display on the Index
view, and also has an "empty" Post
model that can be used to create a new post in the _Create
partial view. I prefer this method, but it's your call.
您的视图模型:
public class MyViewModel
{
public IEnumerable<Post> Posts { get; set; }
public Post CreatePost { get; set; }
}
您首页
操作方法:
public ActionResult Index()
{
MyViewModel model = new MyViewModel()
{
Posts = db.Posts.ToList(),
CreatePost = new MyProject.Models.Post()
};
return View(model);
}
您首页
查看:
@model The.Namespace.MyViewModel
@{
ViewBag.Title = "Index";
}
@foreach (var post in Model.Posts)
{
<p>post.Content</p> // Or however you want to display your posts
}
@Html.Partial("_Create", Model.CreatePost) // Pass the correct model
您 _create
部分观点将保持不变。
Your _Create
partial view will remain the same.
这篇关于我的[建立&QUOT;查看我的&QUOT;指数&QUOT;看来,这可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!