C# 我在一个视图中混合了创建视图和索引视图代码。那么,如何在asp.net MVC4中通过该视图从数据库检索数据呢?

C# 我在一个视图中混合了创建视图和索引视图代码。那么,如何在asp.net MVC4中通过该视图从数据库检索数据呢?,c#,asp.net-mvc,asp.net-mvc-4,razor,C#,Asp.net Mvc,Asp.net Mvc 4,Razor,我在一个视图中合并了两个视图,一个用于创建,另一个用于显示。因此,新视图将能够同时添加和检索数据。 但它只能在数据库中创建新行,而无法检索任何内容。数据库中有4行 这是我的新视图索引:我添加了h1>Works/h1>以确保foreach工作 Index.cshtml 在后期操作中,您没有在视图模型上设置MovieCollection的值。这些信息在请求中无法保存,而且您甚至不接受文章中的完整视图模型(如果接受的话)。因此,在返回视图之前,只需在post操作中重新填充列表。在post操作中,您没有

我在一个视图中合并了两个视图,一个用于创建,另一个用于显示。因此,新视图将能够同时添加和检索数据。 但它只能在数据库中创建新行,而无法检索任何内容。数据库中有4行

这是我的新视图索引:我添加了h1>Works/h1>以确保foreach工作

Index.cshtml


在后期操作中,您没有在视图模型上设置MovieCollection的值。这些信息在请求中无法保存,而且您甚至不接受文章中的完整视图模型(如果接受的话)。因此,在返回视图之前,只需在post操作中重新填充列表。

在post操作中,您没有在视图模型上设置MovieCollection的值。这些信息在请求中无法保存,而且您甚至不接受文章中的完整视图模型(如果接受的话)。因此,在返回视图之前,只需在post操作中重新填充列表即可。

1。在实体文件夹的模型中,添加可枚举的行列表,以便与foreach一起使用:

3.编辑索引页以包含两个视图,一个用于添加,另一个用于检索,如下所示:

@

@

一,。在实体文件夹的模型中,添加可枚举的行列表,以便与foreach一起使用:

3.编辑索引页以包含两个视图,一个用于添加,另一个用于检索,如下所示:

@

@


请按“编辑”键,将准确的问题添加到问题中。@Jamie完成。在查看代码之后。您能按“编辑”将确切的问题添加到您的问题中吗?@Jamie完成。在查看代码之后。我想我已经开始行动了!!'MovieCollection=db.Movies.ToList“不起作用?这对get操作有效。您的post操作中需要相同的内容。您的意思是在我的post操作的同一变量中添加'MovieCollection=db.Movies.ToList'。像这样的'var model=newmovieviewmodel{MovieCollection=db.Movies.ToList,Movie=Movie,};'?不起作用。请给我提供更多的细节,我是初学者。我想我在行动中做到了!!'MovieCollection=db.Movies.ToList“不起作用?这对get操作有效。您的post操作中需要相同的内容。您的意思是在我的post操作的同一变量中添加'MovieCollection=db.Movies.ToList'。像这样的'var model=newmovieviewmodel{MovieCollection=db.Movies.ToList,Movie=Movie,};'?不起作用。请给我提供更多的细节,我是初学者。@Akramalshamei这不一样,但它符合我的需要。非常感谢。你提供的代码足够了,你不需要上传文件,你应该删除它。@Akramalshamei这不一样,但它符合我的需要。非常感谢。你提供的代码足够你不需要上传文件,你应该删除它。
    @model ReMvc.ViewModel.MovieViewModel
    @using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Movie</legend>
        <div class="editor-label">
            @Html.LabelFor(movie => movie.Movie.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(movie => movie.Movie.Title)
            @Html.ValidationMessageFor(movie => movie.Movie.Title)
        </div>

        <div class="editor-label">
            @Html.LabelFor(movie => movie.Movie.ReleaseDate)
        </div>
        <div class="editor-field">
            @Html.EditorFor(movie => movie.Movie.ReleaseDate)
            @Html.ValidationMessageFor(movie => movie.Movie.ReleaseDate)
        </div>
        <div class="editor-label">
            @Html.LabelFor(movie => movie.Movie.Genre)
        </div>
        <div class="editor-field">
            @Html.EditorFor(movie => movie.Movie.Genre)
            @Html.ValidationMessageFor(movie => movie.Movie.Genre)
        </div>

        <div class="editor-label">
            @Html.LabelFor(movie => movie.Movie.Price)
        </div>
        <div class="editor-field">
            @Html.EditorFor(movie => movie.Movie.Price)
            @Html.ValidationMessageFor(movie => movie.Movie.Price)
        </div>
        <p>
            <input type="submit" value="Create" />

        </p>
    </fieldset>
       foreach (var item in Model.MovieCollection)
        {
            <tr>
                <h1>Works</h1>
                <td>
                    @Html.Display(item.Title)
                </td>
                <td>
                    @Html.Display(item.ReleaseDate.ToString())
                </td>
                <td>
                    @Html.Display(item.Genre)
                </td>
                <td>
                    @Html.Display(item.Price.ToString())
                </td>
            </tr>
    }

    }
    <p>
        </p>
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }
    using System;
    using System.Data.Entity;

    namespace ReMvc.Models
    {
        public class Movie
        {
            public int ID { get; set; }
            public string Title { get; set; }
            public DateTime ReleaseDate { get; set; }
            public string Genre { get; set; }
            public decimal Price { get; set; }
        }

        public class MovieDbContext : DbContext
        {
            public DbSet<Movie> Movies { get; set; }
        }
    }
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;

 namespace ReMvc.ViewModel
 {
    public class MovieViewModel
    {
        public ReMvc.Models.Movie  Movie  { get; set; }
        public IEnumerable<ReMvc.Models.Movie>  MovieCollection { get; set;      }
    }
}
  using System;
  using System.Collections.Generic;
  using System.Data;
  using System.Data.Entity;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using ReMvc.Models;
  using ReMvc.ViewModel;

 namespace ReMvc.Controllers
 {
    public class MoviesController : Controller
    {
        private MovieDbContext db = new MovieDbContext();
        //
        // GET: /Movies/

        public ActionResult Index()
        {

            var model = new MovieViewModel()
            { MovieCollection = db.Movies.ToList(), };
            return View(model);
        }

        //
        // POST: /Movies/Index

        [HttpPost]
        public ActionResult Index(Movie movie)
        {
            if (ModelState.IsValid)
            {
                db.Movies.Add(movie);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            var model = new MovieViewModel()
            {
                Movie = movie,
            };
            return View(movie);
        }

    }
public partial class UserProfile
    {
        public int UserId { get; set; }
        public string UserName { get; set; }
        public string PostContent { get; set; }
        public IEnumerable<workingOnAddPost.Entity.UserProfile> UserProfilesCollection { get; set; }
public class HomeController : Controller
{
    private AddingPostEntities db = new AddingPostEntities();

    [HttpGet]
    public ActionResult Index()
    {
        ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

        //return View( db.UserProfiles.ToList());


        var model = new UserProfile() 
        {
            UserProfilesCollection = db.UserProfiles.ToList(), 
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(UserProfile userprofile)
    {
        if (ModelState.IsValid)
        {
            db.UserProfiles.Add(userprofile);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(userprofile);
    }
       //
    // POST: /PostManager/Create
    public ActionResult About()
    {
        ViewBag.Message = "Your app description page.";

        return View();
    }

    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }

    // GET: /PostManager/Details/5

    public ActionResult Details(int id = 0)
    {
        UserProfile userprofile = db.UserProfiles.Find(id);
        if (userprofile == null)
        {
            return HttpNotFound();
        }
        return View(userprofile);
    }


    //
    // GET: /PostManager/Edit/5

    public ActionResult Edit(int id = 0)
    {
        UserProfile userprofile = db.UserProfiles.Find(id);
        if (userprofile == null)
        {
            return HttpNotFound();
        }
        return View(userprofile);
    }

    //
    // POST: /PostManager/Edit/5

    [HttpPost]
    public ActionResult Edit(UserProfile userprofile)
    {
        if (ModelState.IsValid)
        {
            db.Entry(userprofile).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(userprofile);
    }

    //
    // GET: /PostManager/Delete/5

    public ActionResult Delete(int id = 0)
    {
        UserProfile userprofile = db.UserProfiles.Find(id);
        if (userprofile == null)
        {
            return HttpNotFound();
        }
        return View(userprofile);
    }

    //
    // POST: /PostManager/Delete/5

    [HttpPost, ActionName("Delete")]
    public ActionResult DeleteConfirmed(int id)
    {
        UserProfile userprofile = db.UserProfiles.Find(id);
        db.UserProfiles.Remove(userprofile);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    protected override void Dispose(bool disposing)
    {
        db.Dispose();
        base.Dispose(disposing);
    }

}
        @using System.Linq;
    @model  workingOnAddPost.Entity.UserProfile
    @{

    };

    @*<p>*@

<script src="~/Scripts/jquery-2.1.1.min.js"></script>
<script src="~/Scripts/masonry.pkgd.min.js"></script>
<script src="~/Scripts/myScript.js"></script>

<link href="~/Content/Styles/font-awesome.css" rel="stylesheet" />
<link href="~/Content/Styles/main.css" rel="stylesheet" />
@*<div class="Button addcontent">
      <button type="button" >
          <em class="icon-plus"></em>
      </button>
</div>*@
    @section featured {
        <section class="featured">
            <div class="content-wrapper">
                <hgroup class="title">
                    <h1>@*ViewBag.Title.*@</h1>
                    <h2>@*ViewBag.Message*@</h2>
                </hgroup>
                <p>
                    To learn more about ASP.NET MVC visit
                    <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
                    The page features <mark>videos, tutorials, and samples</mark> to help you get the most from ASP.NET MVC.
                    If you have any questions about ASP.NET MVC visit
                    <a href="http://forums.asp.net/1146.aspx/1?MVC" title="ASP.NET MVC Forum">our forums</a>.
                </p>
            </div>
        </section>
    }

    <div class="Button addcontent">
        <button type="button">
            <em class="icon-plus"></em>
        </button>
    </div>

        @using (Html.BeginForm())
            {
            @Html.ValidationSummary(true)


            <div class="modal-addcontent">
                <div class="modal">
                    <div class="standardForm">
                        <h1>
                            Add new content
                        </h1>
                        <ul>
                            <li>
                                <h3>
                                    <label>
                                        Id=
                                    </label>
                                </h3>
                                <div class="editor-field">
                                    @Html.EditorFor(model => model.UserId)
                                    @Html.ValidationMessageFor(model => model.UserId)
                                </div>
                            </li>
                            <li>
                                <h3>
                                    <label>
                                        Name:
                                    </label>
                                </h3>
                                <div class="editor-field">
                                    @Html.EditorFor(model => model.UserName)
                                    @Html.ValidationMessageFor(model => model.UserName)
                                </div>
                            </li>
                            <li>
                                <h3>
                                    <label>
                                        Description
                                    </label>
                                </h3>

                                <div class="content">
                                    @Html.TextAreaFor(model => model.PostContent)
                                    @Html.ValidationMessageFor(model => model.PostContent)
                                </div>
                            </li>
                            <li>
                                <h3>
                                    <label>
                                        Image
                                    </label>
                                </h3>
                                <div>
                                    <input id="upload" type="file" />
                                </div>
                            </li>
                        </ul>
                        <div class="formFooter">
                            <div class="FooterButtons">
                                <button class="Button btn close-modalpopup" type="button">
                                    <span class="buttonText">
                                        Cancel
                                    </span>
                                </button>
                                <p>
                                    <button class="Button primary btn" type="submit" value="Create">
                                        <span class="buttonText">
                                            Create
                                        </span>
                                    </button>

                                </p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            }
<h3>We suggest the following:</h3>
<ol class="round">
    <li class="one">
        <h5>Getting Started</h5>
        ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that
        enables a clean separation of concerns and that gives you full control over markup
        for enjoyable, agile development. ASP.NET MVC includes many features that enable
        fast, TDD-friendly development for creating sophisticated applications that use
        the latest web standards.
        <a href="http://go.microsoft.com/fwlink/?LinkId=245151">Learn more…</a>
    </li>

    <li class="two">
        <h5>Add NuGet packages and jump-start your coding</h5>
        NuGet makes it easy to install and update free libraries and tools.
        <a href="http://go.microsoft.com/fwlink/?LinkId=245153">Learn more…</a>
    </li>

    <li class="three">
        <h5>Find Web Hosting</h5>
        You can easily find a web hosting company that offers the right mix of features
        and price for your applications.
        <a href="http://go.microsoft.com/fwlink/?LinkId=245157">Learn more…</a>
    </li>
</ol>
@*<table>*@
@foreach (var item in Model.UserProfilesCollection)
{

    <div id="main">
        <div id="main-inner">
            <div id="container" class="js-masonry" data-masonry-options='{ "columnWidth": ".grid-sizer", "itemSelector": ".item"}'>
                <div class="grid-sizer">
                <div class="item open-modal">
                    <p>
                        @Html.DisplayTextFor(Modelitem => item.PostContent)
                    </p>
                    </div>
                </div>
            </div>
        </div>
    </div>
}
        @* </table>*@