Reputation: 5069
In my application I am creating a record in one view & displaying it in other view. bellow are controller actions
[HttpPost]
public ActionResult Create(Domain.Entities.Survey survey)
{
ISurveyRepository surveyRepository = new DbSurveyRepository();
surveyRepository.CreateSurvey(survey);
TempData.Add("surveyID",survey.ID);
return RedirectToAction("SingleSurvey");
}
public ActionResult SingleSurvey()
{
if (TempData["surveyID"] != null)
{
ISurveyRepository surveyRepository = new DbSurveyRepository();
Domain.Entities.Survey survey = surveyRepository.GetBySurveyID((int) TempData["surveyID"]);
return View(survey);
}
return View();
}
There are two views 1. Create 2. SingleSurvey
Now when I return view "SingleSurvey" from action "SingleSurvey" the URL displayed on browser is http://localhost:49611/SingleSurvey
.
But I want to change this URL. What I want is http://localhost:49611/SingleSurvey/{my record id}/{my record title}
Is there any way to do this ?
Upvotes: 0
Views: 4039
Reputation: 11342
In your route config file add the following route:
routes.MapRoute(
"SingleSurvey",
"{controller}/{action}/{id}/{title}",
new { controller = "Survey", action = "SingleSurvey", id = UrlParameter.Optional, title = UrlParameter.Optional }
);
Then update the create action to pass the ID and title as part of the route values:
[HttpPost]
public ActionResult Create(Domain.Entities.Survey survey)
{
ISurveyRepository surveyRepository = new DbSurveyRepository();
surveyRepository.CreateSurvey(survey);
TempData.Add("surveyID",survey.ID);
return RedirectToAction("SingleSurvey", new { id = survey.Id, title = survey.Title );
}
In additon, rather than using the TempData to pass the ID it is better to simply read the ID from the URL. To do this, update the SingleSurvey
action to take in the ID as a parameter:
public ActionResult SingleSurvey(int? id)
{
if (id != null)
{
ISurveyRepository surveyRepository = new DbSurveyRepository();
Domain.Entities.Survey survey = surveyRepository.GetBySurveyID(id.Value);
return View(survey);
}
return View();
}
The MVC framework automatically binds the id
parameter defined in the route to the id
parameter on the action method. If you need to use the title in the SingleSurvey action you can also add it as an extra parameter.
Upvotes: 2
Reputation:
Add 2 nullable parameters to the get method
public ActionResult SingleSurvey(int? id, string title)
{
// if id is not null, get the survey based on id
return View(survey);
}
then in the post method, redirect with the parameters
[HttpPost]
public ActionResult Create(Domain.Entities.Survey survey)
{
ISurveyRepository surveyRepository = new DbSurveyRepository();
surveyRepository.CreateSurvey(survey);
return RedirectToAction("SingleSurvey", new { id= survey.ID, title = survey.Title });
}
You might also want want to define a route so that the url is .../SingleSurvey/someID/someTitle
rather than .../SingleSurvey?id=someID&title=someTitle
Side note: Its better performance to initialize a new instance of Survey
and use return View(survey);
rather than return View()
in the case where you are creating a new survey
Upvotes: 1