asp.net-mvc - MVC 3 - 将两个参数从 View 传递到 Controller

标签 asp.net-mvc asp.net-mvc-3 razor

将 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&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.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/

相关文章:

c# - Razor 中仅对一个部分 View 进行分页

css - 左栏不显示

c# - AutoMapper.dll 中出现“AutoMapper.AutoMapperMappingException”

c# - 带有可选参数的 MVC 操作——哪个更好?

asp.net-mvc - 如何将 HTML5 表单操作链接到 ASP.NET MVC 4 中的 Controller ActionResult 方法

c# - 多表单、单 View 、ViewModel for MVC Core 2.0

mysql - 为什么 MySQL 对我来说这么慢?

asp.net-mvc-3 - ASP.NET MVC 3 Controller 路由 - 使主 Controller 下的所有内容都出现在域下

.net - 管理 IIS7 中的并发连接

javascript - MVC Razor 中的级联下拉验证失败