This project is read-only.

"Checkbox" or "Button" column? (newbie)

Mar 14, 2013 at 2:54 PM
Can I define a column to be a "checkbox" or a "button"?

At present, I'm using an Html.ActionLink to act as a checkbox/button. But is there a better way?

Mar 14, 2013 at 4:09 PM
I am not sure that that I understand your question. Can you explain in more details?
Mar 14, 2013 at 5:16 PM
I have a view that contains a Grid.MVC

One of the columns needs to act as a "button". When that column is checked, the form needs to be POSTed, and the controller action needs to be delete the row from the database and then refresh/redisplay the view.

I earlier tried defining the column as:
        columns.Add().RenderValueAs(o = HtmlActionLink("Delete", "DeleteRow", "MyController", new { id = o.Id}, null).ToHtmlString());
... and have the DeleteRow controller action attempt to redisplay the view. But that was a can of worms.

Then I learned about the post/redirect/get pattern, which (if I understand correctly) would have me POST the form when the button is clicked.

So... how would I add a column to the grid that contains a form submit button? Would I simply store an <input type="submit" > tag in the column? Or....???


Mar 14, 2013 at 5:46 PM

Yes, it is not safe to delete items on the GET request. There are two way to solve this problem:

You can render form and submit button in the each row of the grid, using Razor html helper, like:
@helper RenderDeleteForm(Order item)
    <form method="POST" action="@Url.Action("DeleteRow", "MyController", new { id = item.Id})">
        <input type="submit" value="Delete" />
columns.Add().RenderValueAs(o => RenderDeleteForm(o));
The second way - is to redirect (by action link) to separate page that confirms delete operation. On this page you can place your form with delete submit button.
Mar 14, 2013 at 5:53 PM
I just now was reading an earlier post in this forum that described the same solution, but it used action="@Url.RouteUrl... Will either work?

Mar 14, 2013 at 5:58 PM

But if you want to route link to controller action Url.Action - more preferred option.
Mar 15, 2013 at 4:23 PM
I've run into a problem with using a form in a column...

I have a ShowGrid action that initially displays the view containing the grid. ShowGrid is passed the FormCollection via model binding.
  public ActionResult ShowGrid(FormCollection formCollection) 
ShowGrid uses several values in the FormCollection to determine what to display in the grid.

Now the user clicks the column holding the RenderDeleteForm defined as follows:
   @helper RenderDeleteForm(int id)
       <form method="POST" action="@Url.Action("DeleteRow", "MyController", new { id = id})">
           <input type="submit" value="Delete" />
   columns.Add().RenderValueAs(o => RenderDeleteForm(o.Id));

The DeleteRow action deletes the row and then does RedirectToAction("ShowGrid") (per Post-Redirect-Get pattern)
  public ActionResult DeleteRow(int idToDelete)
       // delete row from database using idToDelete
      return RedirectToAction("ShowGrid");
But the FormCollection that ShowGrid gets is not the "parent" form, but the collection for the form in the "delete" column.

Yes, I suppose I could pass all the form variables I need to RenderDeleteForm, and then pass them via the POST

But isn't there a cleaner way?

Mar 17, 2013 at 3:24 PM
No, if you want to restore preveouse state, you need to pass all variables. Of couse, you can store state in session or cookies, but this is design question...