2013-05-17 139 views
1

将视图分配为按钮时,将视图传递给控制器​​时存在一个问题。如果我在我看来,使用此代码:MVC 3 - 将两个参数从视图传递到控制器

@using (Html.BeginForm("Edit", "Shift", new { lineName = item.Line, dateTime=item.Date })) 
     {      
     <input type="submit" value="Edit"/> 
     } 

我得到这个字符串作为结果,因为符号被替换&

<form action="/Shift/Edit?lineName=Line%203&amp;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.W eb.IHtmlString“:在使用语句中使用的类型必须是隐式转换为‘System.IDisposable的’

我的控制器metdhods:(编辑)

//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); 
     } 
    } 

可否请你支持我这个,我现在花几天时间在这一个上。

谢谢

回答

2

根据我对你的代码的了解,我建议你以下解决方案:

在View:

@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) 
{ 
} 

请纠正我,如果我错了。

+0

不幸的是仍然无法正常工作。 @using带下划线,我得到相同的错误 – Whistler

+0

而我的控制器看起来与您写的完全一样。 – Whistler

+0

我已经更新了代码,请尝试这个。仍然有任何错误,请让我知道。 –

0

在控制器方法中添加参数例如:

View :- 
@using (Html.BeginForm("Edit", "Shift", new { lineName = item.Line, dateTime=item.Date })) 
{      
    <input type="submit" value="Edit"/> 
} 

Controller :- 
public ActionResult yourMethod(datatype lineName , datatype dateTime) 
+0

我有两个名字相同但参数个数不同的方法,当我执行代码时,它将我指向一个参数的方法。 – Whistler

相关问题