在编辑结果数据中的MVC3的Controller中没有传递回View(In the Controller of the MVC3 in the Edit Result Data not being passed back to the View)
我创建了一个下拉列表,可以很好地传递数据。 但是,当我想编辑表单时,GET操作中的旧值不会填充在表单中。 我是MVC的新手,所以一个简洁的例子会很棒。 更新 - 我想我知道最新情况并需要帮助解决。 ddl只包含我的下拉数据,因此我只将下拉数据传递给编辑视图中的模型。 表单的数据在我的代码中的变量模型中,如何在视图中传递ddl和模型?
以下是控制器中的代码
public ActionResult Edit(int id) { var ddl = new Users(); ddl.DropDowns = userRepository.Getddl("Departments").Select(c => new SelectListItem { Value = c.DropdownID.ToString(), Text = c.DropdownText }); var model = userRepository.GetUserId(id); return View(ddl); } [HttpPost] public ActionResult Edit(Models.Users users) { userRepository.UpdateUser(users); return RedirectToAction("Index"); }
视图
@model Models.Users @{ ViewBag.Title = "Edit"; } <h2>Edit</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) <div id="stylized" class="myform"> <fieldset> <legend>User Account</legend> <div class="editor-label"> @Html.LabelFor(model => model.UserFullName) </div> <div class="editor-field"> @Html.EditorFor(model => model.UserFullName) @Html.ValidationMessageFor(model => model.UserFullName) </div> <br/> <div class="editor-label"> @Html.LabelFor(model => model.UserName) </div> <div class="editor-field"> @Html.EditorFor(model => model.UserName) @Html.ValidationMessageFor(model => model.UserName) </div> <br/> <div class="editor-label"> @Html.LabelFor(model => model.ProfileName) </div> <div class="editor-field"> @Html.DropDownListFor(x=> x.DeptID,new SelectList(Model.DropDowns,"Value","Text"),"Select") </div>
I created a dropdownlist that passes that data fine. However when I want to edit the form the old values in the GET operation wont populate in the Form. I am new to MVC so a concise example would be great. Update - I think I know whats going on and need help solving. The ddl only contains my dropdown data, so I am passing only the dropdown data to the Model in the Edit View. The data for the form is in the variable model below in my code , how can I pass the ddl and the model in the View?
HERES MY CODE IN THE CONTROLLER
public ActionResult Edit(int id) { var ddl = new Users(); ddl.DropDowns = userRepository.Getddl("Departments").Select(c => new SelectListItem { Value = c.DropdownID.ToString(), Text = c.DropdownText }); var model = userRepository.GetUserId(id); return View(ddl); } [HttpPost] public ActionResult Edit(Models.Users users) { userRepository.UpdateUser(users); return RedirectToAction("Index"); }
VIEW
@model Models.Users @{ ViewBag.Title = "Edit"; } <h2>Edit</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) <div id="stylized" class="myform"> <fieldset> <legend>User Account</legend> <div class="editor-label"> @Html.LabelFor(model => model.UserFullName) </div> <div class="editor-field"> @Html.EditorFor(model => model.UserFullName) @Html.ValidationMessageFor(model => model.UserFullName) </div> <br/> <div class="editor-label"> @Html.LabelFor(model => model.UserName) </div> <div class="editor-field"> @Html.EditorFor(model => model.UserName) @Html.ValidationMessageFor(model => model.UserName) </div> <br/> <div class="editor-label"> @Html.LabelFor(model => model.ProfileName) </div> <div class="editor-field"> @Html.DropDownListFor(x=> x.DeptID,new SelectList(Model.DropDowns,"Value","Text"),"Select") </div>
原文:https://stackoverflow.com/questions/12913912
最满意答案
您正在将错误的对象传递给您的视图。 传递从Repositary方法获得的用户对象。 假设您的
GetUserID
方法返回User
类的对象,并且它在您的用户对象中具有DropDowns
属性public ActionResult Edit(int id) { var model = userRepository.GetUserId(id); model.DropDowns = userRepository.Getddl("Departments"). Select(c => new SelectListItem { Value = c.DropdownID.ToString(), Text = c.DropdownText }); return View(model); }
You are passing a wrong object to your view. Pass the user obeject which you get from your Repositary method. Assuming your
GetUserID
method returns an object ofUser
class and it has aDropDowns
property in your user objectpublic ActionResult Edit(int id) { var model = userRepository.GetUserId(id); model.DropDowns = userRepository.Getddl("Departments"). Select(c => new SelectListItem { Value = c.DropdownID.ToString(), Text = c.DropdownText }); return View(model); }
相关问答
更多-
如果检查呈现的源代码,是否可以检查呈现的子模型输入的名称和ID是否与模型层次结构相对应? 我相信你需要EditorFor才能使子模型正确地“命名空间”。 所以在EventModel视图中,使用如下所示的内容: @Html.EditorFor(m => m.Team1Players, "EventPlayers") 但我不确定。 我在MVC框架中遇到过类似的问题。 If you inspect the rendered source, can you check that the names and id' ...
-
您正在将错误的对象传递给您的视图。 传递从Repositary方法获得的用户对象。 假设您的GetUserID方法返回User类的对象,并且它在您的用户对象中具有DropDowns属性 public ActionResult Edit(int id) { var model = userRepository.GetUserId(id); model.DropDowns = userRepository.Getddl("Departments"). ...
-
您最好的选择是打开MVC附带的示例项目。 它曾经被称为Nerd Dinner,但新的是在这里找到的电影数据库 您要研究的一件事是Form View Models。 基本上,您将一个类从操作结果传递回视图,并在视图中读取该类。 像这样的东西; MyModel myFVM = new MyModel myFVM.SomeData = "Some data" Return View(myFVM) 然后你的视图需要继承自类; Inherits="System.Web.Mvc.ViewPage
... -
尝试 function productSelectedCallback() { var prodId = $(this).attr("id"); var targetUrl = '/Product/Details/' + prodId.toString(); $(this).load(targetUrl); } 编辑: 从控制器上更改您的操作方法 public ActionResult Details(string productId) { return View(); } 至 ...
-
隐藏的领域应该起作用。 问题是你的控制器不接受它。 你可以使用ViewModel来实现这一点。 或者,在您的操作中使用以下代码: id = Request.Form["id"] The hidden field should works. The problem is that your controller did not accept it. You can use ViewModel to achieve this. Or, use the code below in your action: id ...
-
您可以查看以下博客文章 。 另请查看以下帖子,以更好地理解ASP.NET MVC中默认模型绑定器使用的列表和词典的预期线路格式。 You may take a look at the following blog post. Also checkout the following post to better understand the expected wire format for lists and dictionaries used by the default model binder in A ...
-
您的表单中似乎没有相应的输入字段用于视图模型的字段。 您的数量文本框的命名也是错误的。 尝试使用以下内容替换视图中的foreach循环: @for (int i = 0; i < Model.items.Count; i++) { @Html.HiddenFor(x => x.items[i].ItemID) @Html.HiddenFor(x => x.items[i].ItemName) @Html.HiddenFor(x => x.items[i].Description) ...
-
清除模型状态: if (TryUpdateModel(new_comment)) { ModelState.Clear(); return View(new Comment()); //no more problem here } 所有HTML帮助程序在呈现它们的值时首先查看ModelState,然后仅在模型中查看。 甚至更好,更正确地使用Redirect-After-Post模式(即成功POST后重定向): if (TryUpdateModel(new_comment)) { r ...
-
由于它们不是您要发布的形式 - (导出到Excel是单独的形式)。 输入 FromDate,ToDate和SearchWord 是第一种形式(在局部视图中)。 所以这些值不会出现在控制器中(因为它们不是http帖子的一部分)。 如果您希望将所有这些值传递回控制器,它们应该在一个值下 Html.BeginForm Since they are not in the form that you are posting - (Export to Excel is in a separate form). The ...
-
在Controller Action MVC3中获取表单字段和模型数据(Get Form Fields and Model Data in a Controller Action MVC3)[2021-06-07]
您可以按name从view到controller访问表单字段 在视图中 在控制器中 public ActionResult YourAction(Model model,string fname) { //Access fname here } 如果您想要从视图中访问多个未绑定到模型的值,可以使用FormCollection public ActionResult YourAction(Model model,FormCollect ...