Skip to main content
 首页 » 编程设计

c#之如何使用 Asp.net MVC4 将两个 View 合并为一个 View

2024年02月05日15mate10pro

我添加了一个新 Controller ,然后我选择了以下选项:

  • 模板:具有读/写操作和 View 的 MVC Controller ,使用 Entity Framework 。
  • 模型类:电影 (MvcMovie.Models)。
  • 数据上下文类: MovieDBContext (MvcMovie.Models)。
  • View :Razor (CSHTML)。

我有一个默认包含 View 的文件夹:Index、Creat、Edit、Detailes 和 Delete。

到这里一切正常。

我的问题是我试图将创建 View 的代码与索引 View 的代码(两个 View 合二为一)合并。

我得到的错误:

编译器错误消息:CS1061:“System.Collections.Generic.IEnumerable”不包含“Title”的定义,并且没有扩展方法“Title”接受类型为“System”的第一个参数。可以找到 Collections.Generic.IEnumerable'(您是否缺少 using 指令或程序集引用?)

这是我的合并 View (Index.cshtml):

@model IEnumerable<ReMvc.Models.Movie> 
 
@{ 
ViewBag.Title = "Index"; 
} 
 
<h2>Index</h2> 
 
@using (Html.BeginForm()) { 
@Html.ValidationSummary(true) 
 
<fieldset> 
    <legend>Movie</legend> 
 
    <div class="editor-label"> 
        @Html.LabelFor(model => model.Title) 
    </div> 
    <div class="editor-field"> 
        @Html.EditorFor(model => model.Title) 
        @Html.ValidationMessageFor(model => model.Title) 
    </div> 
 
    <div class="editor-label"> 
        @Html.LabelFor(model => model.ReleaseDate) 
    </div> 
    <div class="editor-field"> 
        @Html.EditorFor(model => model.ReleaseDate) 
        @Html.ValidationMessageFor(model => model.ReleaseDate) 
    </div> 
 
    <div class="editor-label"> 
        @Html.LabelFor(model => model.Genre) 
    </div> 
    <div class="editor-field"> 
        @Html.EditorFor(model => model.Genre) 
        @Html.ValidationMessageFor(model => model.Genre) 
    </div> 
 
    <div class="editor-label"> 
        @Html.LabelFor(model => model.Price) 
    </div> 
    <div class="editor-field"> 
        @Html.EditorFor(model => model.Price) 
        @Html.ValidationMessageFor(model => model.Price) 
    </div> 
 
    <p> 
        <input type="submit" value="Create" /> 
    </p> 
    </fieldset> 
    } 
 
 @foreach (var item in Model) { 
 <tr> 
    <td> 
        @Html.DisplayFor(modelItem => item.Title) 
    </td> 
    <td> 
        @Html.DisplayFor(modelItem => item.ReleaseDate) 
    </td> 
    <td> 
        @Html.DisplayFor(modelItem => item.Genre) 
    </td> 
    <td> 
        @Html.DisplayFor(modelItem => item.Price) 
    </td> 
 </tr> 
} 
 
</table> 

这是我的 Controller : 我将“Create”ActionResult 重命名为 Index

namespace ReMvc.Controllers 
{ 
public class MoviesController : Controller 
{ 
    private MovieDbContext db = new MovieDbContext(); 
 
    // 
    // GET: /Movies/ 
 
    public ActionResult Index() 
    { 
        return View(db.Movies.ToList()); 
    } 
 
 
 
    [HttpPost] 
    public ActionResult Index(Movie movie) 
    { 
        if (ModelState.IsValid) 
        { 
            db.Movies.Add(movie); 
            db.SaveChanges(); 
            return RedirectToAction("Index"); 
        } 
 
        return View(movie); 
    } 
} 

这是我的模型: 使用系统; 使用 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; } 
} 
} 

我是 MVC 的初学者,我在这里呆了超过 4 天.. 请帮忙。

请您参考如下方法:

开始你的model是 IEnumerable<ReMvc.Models.Movie> 的一种.

所以它在提示 IEnumerable<T>没有名为 Title 的方法或属性:

 @Html.LabelFor(model => model.Title) 

'System.Collections.Generic.IEnumerable' does not contain a definition for 'Title' and no extension method 'Title' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found

如果您不想使用 IEnumerable<T>对于模型,将其更改为:

@model ReMvc.Models.Movie 

如果您确实打算使用 IEnumerable<T> 的类型那么您将需要遍历您的项目以访问它们:

@using (Html.BeginForm()) { 
@Html.ValidationSummary(true) 
@foreach(var movie in Model) 
{ 
<fieldset> 
    <legend>Movie</legend> 
 
    <div class="editor-label"> 
        @Html.Label(movie.Title) 
    </div> 
    <div class="editor-field"> 
        @Html.Editor(movie.Title) 
        @Html.ValidationMessage(movie.Title) 
    </div> 
} 

我还建议您不要在表示层(您的 Controller )中访问数据。考虑将数据访问与 UI 分开。

如果你想使用 IEnumerable<T> 的类型和一种类型 ReMvc.Models.Movie在同一个观点?您将必须执行类似于以下的操作。

您可以创建一个新类型并使用它:

public class MovieViewModel() 
{ 
    public ReMvc.Models.Movie Movie { get; set; } 
    public IEnumerable<ReMvc.Models.Movie> MovieCollection { get; set; } 
} 

然后你可以将它传递到你的 View 中

@model MovieViewModel 

在你的 Controller 中你只需这样做:

public ActionResult Index() 
{ 
    var model = new MovieViewModel()  
    { 
        MovieCollection = db.Movies.ToList(), 
    }; 
    return View(model); 
} 
 
[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); 
} 

这样做的缺点是您认为您现在需要检查一个是否为空(因为在您的代码中,一个属性将始终为空 Movie 或 MovieCollection )。我建议拆分您的 View 并使用不同的操作方法名称,例如详情。