简体   繁体   English

我是否需要在填充下拉列表时将数据从控制器传递到视图?

[英]Do I need to pass data from the controller to the view in populating a dropdownlist?

I'm currently studying asp.net mvc and I just started, I decided to move away from web forms to mvc. 我正在学习asp.net mvc,我刚开始,我决定从网络表格转移到mvc。

I'm just curious because I have this code and I want to know the difference between passing the model in the return View(data) and not passing it. 我只是很好奇,因为我有这个代码,我想知道在return View(data)传递模型与不传递它之间的区别。

Here's the code: 这是代码:

The Controller 控制器

/* Even if I comment/remove the lines ViewBag.GenreId....... and ViewBag.ArtistId
   and just return View(); everything seems to work fine. I'm following this music store tutorial from codeplex
*/

[HttpPost]
public ActionResult Create(Album album)
{
     if (ModelState.IsValid)
     {
          db.Albums.Add(album);
          db.SaveChanges();
          return RedirectToAction("Index");  
     }
     //this will assign the values of the dropdownlist of the View
     //it will assign the values on the dropdownlist that has a name GenreId
     ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
     ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
     return View(album);
}

The Code for the View 观点代码

@model CodeplexMvcMusicStore.Models.Album

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Album</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.GenreId, "Genre")
        </div>
        <div class="editor-field">
            @Html.DropDownList("GenreId", String.Empty)
            @Html.ValidationMessageFor(model => model.GenreId)
        </div>

I would also like to know the difference between passing in the object model in View(album) vs not passing it View() . 我还想知道在View(album)传递对象模型与传递View()之间的区别。

As far as I am aware, if you do not pass the model through then your page will not be populated. 据我所知,如果您没有通过该模型,那么您的页面将不会被填充。

Also it will not know where to bind the values back to when you post the form back. 此外,当您发布表单时,它也不知道将值绑定到何处。

If you dont pass the data then you cannot access the data in the razor. 如果您没有传递数据,则无法访问剃刀中的数据。 You need to pass your model to the return View(model) in order to use it on the Razor View. 您需要将模型传递给return View(model)才能在Razor View上使用它。 If you need to pass more than one model then you can pass you use either ViewBag or ViewData to do this. 如果您需要传递多个模型,那么您可以使用ViewBagViewData来执行此操作。

By looking at your question. 通过查看你的问题。 It seems to me you might be able to find your answer in this MVC DropDownListFor tutorial 在我看来,你可以在这个MVC DropDownListFor教程中找到你的答案

If you do not pass object model in return View(album) it will not show any validation errors in your view if there are any. 如果您没有在返回View(相册)中传递对象模型,则在视图中不会显示任何验证错误(如果有)。 As you are using ViewBag for GenreId and ArtistId you can render in view without passing the object model to view (return View()) posted by Karthik 当您使用ViewBag for GenreId和ArtistId时,您可以在视图中渲染而不会将对象模型传递给Karthik发布的视图(返回View())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM