- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面是我的代码。
型号
public class ShiftsModel
{
public string UID { get; set; }
public string Date { get; set; }
public string Time { get; set; }
public string Location { get; set; }
}
Controller
public class HomeController : Controller
{
public string xmlPath = HostingEnvironment.MapPath("~/App_Data/data.xml");
public ActionResult Index()
{
XDocument xml = XDocument.Load(xmlPath);
var shifts = (from b in xml.Descendants("Shift")
select new ShiftsModel
{
UID = (string)b.Attribute("UID"),
Date = (string)b.Element("Date"),
Time = (string)b.Element("Time"),
Location = (string)b.Element("Location")
}).ToList();
return View(shifts);
}
}
我现在想在我的 Index.cshtml 文件中引用它,如下所示:
@foreach(var shift in (List<object>ViewBag.shifts)) {
<tr>
<td>
<input type="text" id="date" name="date" placeholder="Date" value="@(ViewBag.date)" }>
</td>
<td>
<input type="text" id="time" name="time" placeholder="Shift time" value="@(ViewBag.time)" }>
</td>
<td>
<input type="text" id="location" name="location" placeholder="Location" value="@(ViewBag.location)" }>
</td>
</tr>
}
但是,我在 List<object>ViewBag.shifts
上收到错误消息台词:
Represents a strongly typed list of objects that can be accessed by index.
请问我做错了什么?谢谢:)
最佳答案
如我所见,您只是不通过 Controller 中的 ViewBag
将您的集合传递给 View。
你应该像这样传递它:
public ActionResult Index()
{
XDocument xml = XDocument.Load(xmlPath);
var shifts = (from b in xml.Descendants("Shift")
select new ShiftsModel
{
UID = (string)b.Attribute("UID"),
Date = (string)b.Element("Date"),
Time = (string)b.Element("Time"),
Location = (string)b.Element("Location")
}).ToList();
ViewBag.shifts = shifts; // this line will pass your object
return View();
}
然后在你的 View 上:
@foreach(var shift in (List<ShiftsModel>ViewBag.shifts)) {
<tr>
<td>
<input type="text" id="date" name="date" placeholder="Date"
value="@(shift.Date)" }>
</td>
<td>
<input type="text" id="time" name="time" placeholder="Shift time"
value="@(shift.Time)" }>
</td>
<td>
<input type="text" id="location" name="location" placeholder="Location"
value="@(shift.Location)" }>
</td>
</tr>
}
但是 MVC 解决问题的方法是像这样使用强类型 View :
Controller :
public ActionResult Index()
{
XDocument xml = XDocument.Load(xmlPath);
var shifts = (from b in xml.Descendants("Shift")
select new ShiftsModel
{
UID = (string)b.Attribute("UID"),
Date = (string)b.Element("Date"),
Time = (string)b.Element("Time"),
Location = (string)b.Element("Location")
}).ToList();
ViewData.Model = shifts; // this line will pass your object but now to model
return View();
}
查看:
@model List<ShiftsModel> @*this is where your model is defined on view*@
@for(int i = 0; i < Model.Count(); i++) {
<tr>
<td>
@Html.TextBoxFor(x=> Model[i].Date, new { placeholder = "Date" })
</td>
<td>
@Html.TextBoxFor(x=> Model[i].Time, new { placeholder = "Shift time" })
</td>
<td>
@Html.TextBoxFor(x=> Model[i].Location, new { placeholder = "Location" })
</td>
</tr>
}
如果您要将此模型发布到 Controller ,则需要 for
循环而不是 foreach
来解决数组绑定(bind)的 MVC 问题。
关于c# - Foreach 与来自 viewbag 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32822185/
我需要将一些信息传递给部分 cshtml View 。为此,我使用了 ViewDataDictionary 和 ViewBag。 public ActionResult TaskPage(int ID
我有 RedirectToAction 作为 Controller 中的返回值,在我看来我使用 ViewBag。虽然在 RedirectToAction 之后 ViewBag 变空是一个众所周知的问题
我已经为 HtmlHelper 编写了扩展方法,该方法是从需要访问 ViewBag 以获取页面设置的信息的母版页调用的。 这是方法签名: public static string BuildFavor
我正在尝试在我的应用程序中使用 ViewBag,我拥有所有最近的 dll、最新版本的 MVC 3,但我仍然收到错误: "The name 'ViewBag' does not exist in the
我正在尝试通过 JavaScript 将 ViewBag 中的值插入到输入中并动态创建该值。 我调用 Controller 创建的employeeID的值 public string GetEmplo
我需要显示存储在 ViewBag 中的日期,为此我需要使用 DisplayTemplate。现在,如果它是模型中的日期,我会使用 DisplayFor 助手,但我该如何为 ViewBag 做呢? 我有
我知道网上已经有一些解决方案,但在尝试了所有解决方案后,我对自己感到困惑。 我只想澄清我尝试过的解决方案: 确保 Microsoft.AspNet.Web.Optimization 或 System.
在MSDN documentation ,有 ViewBag.MyMessageToUsers 和 ViewBag.AnswerText。在ASP.net/mvc tutorial , 有 ViewB
这个问题在这里已经有了答案: Can the ViewBag name be the same as the Model property name in a DropDownList? (2 个答
我测试了我的应用程序,在测试时我发现了一个错误,我认为它来自 ViewBage。我有 500 名医生和专科医生的名单,当我想编辑电话、地址等时,下拉菜单会显示列表中第一个专科医生的名字,所以它不会保存
我有一个在数据库中注册聊天的表单,一切正常,问题是 ViewBag,因为这会将消息带到 View 中的 Javascript,以便它可以进行验证。当用户注册时,应该会出现一条成功消息,但在 ViewB
有没有办法清除ViewBag? ViewBag 没有 setter,因此不能简单地取消它: ViewBag = null; 我似乎也不能use reflection迭代它并清除它的动态属性,因为您不能
想象一个名为的 View 包 ViewBag.Modes 这包含以下内容: Simple Advanced Manual Complete 如何像在数组中那样通过索引访问 viewbag? 例如 S
是否可以在调用重定向之前设置 ViewBag? 我想要这样的东西: @ViewBag.Message="MyMessage"; RedirectToAction("MyAction"); 最佳答案 当
我正在做一个项目,我只需要从两个表中获取数据,我想使用 LINQ Query 来获取数据。 在我的 Controller 中: var Parameters = (from test in _db.t
与'12 asp.net C# MVC4 - Int.Appl.Template EF Code First 这是我非常简单的脚本 $('#Add').click(function (e)
这是我的 Controller 代码: const string template = "clm_adj_process_cd ('@[CdCode]',varchar) ~f.src_dm_plat
我在 MVC3 项目中有一个 View ,在加载时我将一个实体设置到 ViewBag 中。 实体的属性之一是 Datetime? 类型。 在 $(document).ready我将 ViewBag 中
我尝试将 ViewBag.OrderId 设置为数据属性 data-orderId 的值,但在 =Add } JS: $(".btn-add-note").click(function(){
我需要显示存储在 ViewBag 中的日期,并且我需要使用 DisplayTemplate 来实现此目的。现在,如果它是模型中的日期,我将使用 DisplayFor 助手,但我该如何为 ViewBag
我是一名优秀的程序员,十分优秀!