MVC 3-将两个参数从视图传递到控制器 [英] MVC 3 - passing two parameters from view to controller
问题描述
将2个参数分配为按钮时,我在将2个参数从视图传递到控制器时遇到问题.如果我在视图中使用此代码:
I have an issue with passing 2 parameters from view to controller, when assigning them as a button. If I use this code in my View:
@using (Html.BeginForm("Edit", "Shift", new { lineName = item.Line, dateTime=item.Date }))
{
<input type="submit" value="Edit"/>
}
我得到此字符串作为结果,该字符串不起作用,因为&符替换为&
I get this string as result, which doesn't work because ampersand is replaced with &
<form action="/Shift/Edit?lineName=Line%203&dateTime=04%2F01%2F2004%2007%3A00%3A00" method="post"> <input type="submit" value="Edit"/>
</form>
为了解决这个问题,我发现可以使用Html.Raw
So to resolve that I found I could use the 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'
'System.Web.IHtmlString': type used in a using statement must be implicitly convertible to 'System.IDisposable'
我的控制器方法:(已编辑)
//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);
}
}
请您为此提供支持,我现在花了几天时间.
Could you please support me with this, I spend couple of days on this one now.
谢谢
推荐答案
根据我对您代码的理解,建议您采用以下解决方案:
According to my understanding of your code, I suggest you following solution:
在视图中:
@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"/>
}
在控制器中:-
[HttpPost]
public ActionResult Edit(datatype lineName , datatype dateTime)
{
}
如果我错了,请纠正我.
Please correct me If I am wrong.
这篇关于MVC 3-将两个参数从视图传递到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!