WISEMAN
WISEMAN

Reputation: 97

redirect to action which send request

I have a controller "Contracts" and 3 GET methods: "All", "Focus", which contains grid, and "Edit" for editing, and one POST method: "Edit". Users can access to Edit from any grid by using button. But after POST executed, i want to redirect them to action that send request. For example:

  1. open "Focus" grid
  2. select row
  3. clicked edit
  4. make changes, and click save
  5. finally redirect to "Focus" grid (!)

or

  1. open "All" grid
  2. select row
  3. clicked edit
  4. make changes, and click save
  5. finally redirect to "All" grid (!)

Upvotes: 1

Views: 277

Answers (2)

Shyju
Shyju

Reputation: 218722

public ActionResul All()
{
  var items=dbContext.Items;
  return View("All",items);
}

in the All View you will have the grid with the data. Selecting a record from the Grid and clicking Edit will take you to the second Action method for Edit.

You may pass a some flag when calling the edit method here. You may add that when you build the edit link like this

@Html.ActionLink("Edit","Edit","Item",new { @id=item.Id, @from="all"},null)

So my Edit will have a querystring key "from" with a value "all"

Same way,in your the View for your Focus, you may pass a different value

@Html.ActionLink("Edit","Edit","Item",new { @id=item.Id, @from="focus"},null)

Now the Edit Action method, You read the parameter and set it as the property value of our edit view model. you may want to add this property to your ViewModel.

public ActionResult Edit(int id,string from)
{
  EditItemViewModel item=GetItem(id);
  item.From=from;      
  return View(item);
}

and this View will have the edit form. We will keep the value of From inside a form element so that we can use that on the form post.

@model EditItemViewModel 

@using(Html.BeginForm())
{
   @Html.HiddenFor(m => m.Id);
   @Html.TextBoxFor(m => m.ItemName)
   @Html.TextBoxFor(m => m.ItemDesc)
   @Html.HiddenFor(m => m.From)
   <input type="submit" value="Update" />
}

Where user can edit and submit it again . you handle that in the HttpPost Edit action method. Check the From property value and decide where to redirect after saving

[HttpPost]
public ActionResult Edit(EditItemViewModel model)
{
  if(ModelState.IsValid)
  {
    //Save data 
   if(model.From=="all")
       return RedirectToAction("All");
   else
       return RedirectToAction("Focus");
  }
  return View(model);

}

Upvotes: 1

maztt
maztt

Reputation: 12294

i think that your post edit can be like this

After clicking on the gridrow having (Edit/All/1 or Edit/Focus/2) you can redirect to this

   public ActionResult Edit(int id, yourviewmodel viewmodel,string YourFilter)
   {

           return RedirectToAction(YourFilter);
   }

and in global.asax you can set your route like this for the edit submit

    routes.MapRoute(
            "filters",
            "All",
            new { controller = "Contracts", action = "All" }
        ); 

       routes.MapRoute(
            "filters1",
            "focus",
            new { controller = "Contracts", action = "focus" }
        ); 

for edit click

      routes.MapRoute(
          "EditFilter",
          "Edit/{YourFilter}/{id}",
          new { controller = "Contract", action = "Edit",YourFilter = UrlParameter.Optional,id = UrlParameter.Optional }
      );

Upvotes: 0

Related Questions