将 2 个参数分配为按钮时,我在将 2 个参数从 View 传递到 Controller 时遇到问题。如果我在 View 中使用此代码:
@using (Html.BeginForm("Edit", "Shift", new { lineName = item.Line, dateTime=item.Date }))
{
<input type="submit" value="Edit"/>
}
我得到这个字符串作为结果,但它不起作用,因为 & 符号被替换为 &
<form action="/Shift/Edit?lineName=Line%203&dateTime=04%2F01%2F2004%2007%3A00%3A00" method="post"> <input type="submit" value="Edit"/>
</form>
因此,为了解决这个问题,我发现我可以使用 Html.Raw
@using (Html.Raw(Url.Action("Edit", "Shift", new { lineName = item.Line, dateTime=item.Date })))
{
<input type="submit" value="Edit"/>
}
但这给了我错误:
“System.Web.IHtmlString”:using 语句中使用的类型必须可隐式转换为“System.IDisposable”
我的 Controller 方法:(已编辑)
//Displays Edit screen for selected Shift
public ViewResult Edit(string lineName, DateTime dateTime)
{
Shift shift = repository.Shifts.FirstOrDefault(s => s.Line == lineName & s.Date == dateTime);
return View(shift);
}
//Save changes to the Shift
[HttpPost]
public ActionResult Edit(Shift shift)
{
// try to save data to database
try
{
if (ModelState.IsValid)
{
repository.SaveShift(shift);
TempData["message"] = string.Format("{0} has been saved", shift.Date);
return RedirectToAction("Index");
}
else
{
//return to shift view if there is something wrong with the data
return View(shift);
}
}
//Catchs conccurency exception and displays collision values next to the textboxes
catch (DbUpdateConcurrencyException ex)
{
return View(shift);
}
}
您能支持我吗?我现在在这个问题上花了几天时间。
谢谢
最佳答案
根据我对您代码的理解,我建议您采用以下解决方案:
查看中:
@using (Html.BeginForm("Edit", "Shift", FormMethod.Post, new { enctype = "multipart/form-data"}))
{
<input type="hidden" name="lineName" value="@item.Line"/>
<input type="hidden" name="dateTime" value="@item.Date"/>
<input type="submit" value="Edit"/>
}
在 Controller 中:-
[HttpPost]
public ActionResult Edit(datatype lineName , datatype dateTime)
{
}
如有错误,请指正。
关于asp.net-mvc - MVC 3 - 将两个参数从 View 传递到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16604735/