MVC 3 - passing two parameters from view to contro

2019-08-14 09:16发布

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&amp;dateTime=04%2F01%2F2004%2007%3A00%3A00" method="post">            <input type="submit" value="Edit"/>
</form>

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"/>
        }

But this give me error:

'System.Web.IHtmlString': type used in a using statement must be implicitly convertible to 'System.IDisposable'

My Controller metdhods: (Edited)

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

Thanks

2条回答
淡お忘
2楼-- · 2019-08-14 09:41

add parameter in controller method for eg.

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)
查看更多
Deceive 欺骗
3楼-- · 2019-08-14 09:58

According to my understanding of your code, I suggest you following solution:

In 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"/>
        }

In Controller :-

     [HttpPost]
public ActionResult Edit(datatype lineName , datatype  dateTime)
{
}

Please correct me If I am wrong.

查看更多
登录 后发表回答