【问题标题】:My "Create" View in my "Index" View, is it possible?我的“索引”视图中的“创建”视图,这可能吗?
【发布时间】:2012-12-13 02:25:53
【问题描述】:

我正在使用带有 EF 的 ASP.NET MVC 4,我有一个带有索引和创建视图的 PostController。我希望将两者都放在同一页面上以添加帖子,并将其可视化在同一页面上。我该怎么做?

感谢您的建议

___编辑__

控制器:

        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);
        }

我的_创建局部视图:

@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 类型的模型项。发布”。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 razor


    【解决方案1】:

    这真的取决于你的代码现在的样子。如果你有一个单独的Create 操作方法返回一个PartialView,像这样:

    [HttpGet]
    public ActionResult Create()
    {
        // Do stuff here to populate your model, if necessary
        return PartialView("_Create", model);
    }
    

    然后在您的视图中,您将使用 Html.RenderAction() 来显示 _Create 部分视图:

    <div id="IndexViewStuff">
        <p>Some stuff in your normal Index view.</p>
        @{Html.RenderAction("Create", "Post");}
    </div>
    

    如果您没有针对 Create 的单独操作方法,并且只有一个局部视图以使事情更清洁,那么只需在您的 Index 视图中使用 Html.Partial() 即可:

    @Html.Partial("_Create") // If your _Create partial does not require a model
    @Html.Partial("_Create", Model.CreateViewModel) // If it does require a model
    

    更新

    查看您的代码后,您可以通过两种方式来实现(我将向您展示这两种方式)。出现您的问题是因为您将帖子列表传递给您的 Index 视图,而您的 _Create 部分视图需要一个 Post 模型。由于您在调用模型时没有将模型显式传递给局部视图,因此它会自动尝试在 Index 视图(您的帖子列表)中使用模型。解决问题的第一种方法需要对您的代码进行最少的更改。

    如下更改您的Create 操作方法:

    [HttpGet]
    public ActionResult Create()
    {
        // You have to pass a new Post
        return PartialView("_Create", new MyProject.Models.Post());
    }
    

    然后在您的Index 视图中,使用:

    @{Html.RenderAction("Create", "Post");}
    

    第二种方法是使用一个视图模型,该模型公开要在Index 视图上显示的帖子列表,并且还有一个“空”Post 模型,可用于在@ 中创建新帖子987654341@部分视图。我更喜欢这种方法,但这是你的决定。

    您的视图模型:

    public class MyViewModel
    {
        public IEnumerable<Post> Posts { get; set; }
        public Post CreatePost { get; set; }
    }
    

    您的Index 操作方法:

    public ActionResult Index()
    {
        MyViewModel model = new MyViewModel()
        {
            Posts = db.Posts.ToList(),
            CreatePost = new MyProject.Models.Post()
        };
    
        return View(model);
    }
    

    您的Index 视图:

    @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 部分视图将保持不变。

    【讨论】:

    • 您好,感谢您的解释。我已经用一些代码更新了我的帖子。我想我需要创建一个 ViewModel 就像你所说的那样,因为它确实需要一个模型。我不知道如何使用 ViewModel,我需要在其中声明什么?我的索引视图使用 IEnumerable 而我的 _create 不使用 IEnumerable,如果我理解错误,那就是冲突的地方......对不起我的英语也......
    • @Alternative 查看我的更新答案,如果您需要进一步说明,请告诉我。
    • 再次感谢您的精确解释,我现在更好地了解它是如何工作的。但是,我对这两种方法都有一些问题。第一个有效,我的部分视图显示得很好,但是当我单击“创建”时,它返回给我child actions are not allowed to perform redirect actions。但是我的数据在数据库中写得很好,并在我重新执行时显示出来。其次,我不太了解 Index action 方法、viewmodel 和 create 方法之间的联系……我试过了,我的局部视图显示得很好,但是当我点击 create 时什么也没有出现。跨度>
    • @Alternative 对于第一个问题,尝试在您的 HttpPost Create 操作方法中使用 return RedirectToAction("Index", "Post");。对于第二个,您是否使用与尝试第一种方法时相同的 _Create 部分视图?
    • 对于第一种方法,即使使用参数“Post”也存在同样的问题。是的,我正在使用相同的 _Create 部分视图。
    猜你喜欢
    • 1970-01-01
    • 2010-12-29
    • 2017-07-06
    • 2011-07-13
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多