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&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 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Unfortunately still doesn't work. @using is underlined and I get the same error
And my controller looks exactly like the one you have written.
I have updated code please try with this. Still there are any error, please let me know.
It is working, but it directs my to the wrong method. I will add my method in controller to the question code.
can you tell me to which method should be post form? So that I will tell you proper solution.
|
0

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)

1 Comment

I have two methods with the same name but different number of paramteres, when I do your code it directs me to the method with one paramenter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.