Calling View present in HomeController from Action Method of otherController - c#

Can anyone please help me? I am trying to call a view which is present in Home Controller directly from the action method of another controller. I don't want to use the return redirectToAction() method, but rather would like to call the view of HomeController directly. In HomeController I have this HttpGet Method:
public ActionResult SuperAdmin()
{
if (Session["User"] != null)
{
TempData["BindGrid"] = objDBContext.tbl_Student.Where(X => X.RegistrationConfirmed == false).ToList();
return View(); //Go to SuperAdmin View if condition is true
}
else
{
return View("Login", "Home"); //else redirect to Login view
}
}
Present Controller from where i would like to call HomeController SuperAdmin View
public ActionResult Search(string Search)
{
if (Session["User"] != null)
{
if (Search != string.Empty)
{
TempData["BindGrid"] = objDBContext.tbl_Student.Where(X => (X.UserName.StartsWith(Search) || Search == null) && (X.RegistrationConfirmed == false));
}
return View("SuperAdmin", "Home"); //If true, go to SuperAdmin view directly
}
else
{
return RedirectToAction("Login", "Home");
}
}

You can directly return a different view like:
return View("yourViewName", Model); //if it is in same controller and if you pass model then pass model in this way
if your view is in different controller then
return View("FolderName/ViewName", Model ); // pass a view of a different controller with model. Just specify view name and it's folder name

Related

GET Method is going to the right page but stills runs through POST method and has the POST in the URL

I have a POST method that returns me to my summary page that looks like this
public ActionResult SpecialOrderSelection(ItemViewModel model)
{
if (ModelState.IsValid)
{
JobOrder jobOrder = db.JobOrders.Find(model.Id);
if (jobOrder == null)
{
return HttpNotFound();
}
ViewBag.JobOrderID = jobOrder.ID;
}
return SpecialOrderSummary(model);
}
And here is my Summary GET method
public ActionResult SpecialOrderSummary(ItemViewModel model)
{
return View("SpecialOrderSummary", model);
}
It sends me to my SpecialOrderSummary View Page, however the URL still displays as what my POST method is and not my GET. Also on every refresh it goes to the post first.
Why is this?
This is normal, because mvc runs like this.
You should use redirect,
public ActionResult SpecialOrderSelection(ItemViewModel model)
{
if (ModelState.IsValid)
{
JobOrder jobOrder = db.JobOrders.Find(model.Id);
if (jobOrder == null)
{
return HttpNotFound();
}
ViewBag.JobOrderID = jobOrder.ID;
}
return Redirect("SpecialOrderSummary?(your model serialize)");;
}

MVC controller's parameter always null

i can't figure out why the username in the controller always returned as null. When i clicked the button, the button works fine and return the member's username.
http://localhost:50555/Account/UserDetails/member1
In my view
<button data-get="/Account/UserDetails/#m.Username">Details</button>
In controller
[Authorize(Roles = "Supervisor, Admin")]
public ActionResult UserDetails(string username)
{
var model = db.Members.Find(username);
if (model == null)
{
return RedirectToAction("ManageUsers");
}
return View(model);
}
You are not taking the value of username from the querystring. You are expecting a route parameter. So you will have to specify the route parameter as shown below:
[Authorize(Roles = "Supervisor, Admin")]
[HttpGet("{username}")]
public ActionResult UserDetails(string username)
{
var model = db.Members.Find(username);
if (model == null)
{
return RedirectToAction("ManageUsers");
}
return View(model);
}
Refer this documentation for more details.

ActionResult to do nothing or render a PartialView given a certain condition

I have this code:
public ActionResult SampleResult()
{
Model model = new Survey();;
.
. Do something with model here
.
if (model.Id == 0)
{
//return EmptyResult();
return DoNothing(); //How?
}
return PartialView("MyView", model);
}
I want to do nothing when model.Id==0
else render partialview.
you can just return same view as like below
public ActionResult SampleResult(Survey surveyModel)
{
Model model = new Survey();;
.
. Do something with model here
.
if (model.Id != 0)
{
return PartialView("MyView", model);
}
return View(surveyModel);
}

Wrong view being returned for controller method

I have a controller method that is returning the wrong view. The view I have is the same name as the controller method "AssignTask.cshtml". The method is "public virtual ActionResult AssignTask(ManageTaskModel model) "
Can anyone see what I'm doing wrong?
[HttpGet]
public virtual ActionResult ManageTasks()
{
try
{
var model = new ManageTaskModel ();
model.assignedPSUsers = Orchestrator.GetAssignedPSUsers();
return View(model);
}
catch (Exception e)
{
ModelState.AddModelError("ErrorMsg", e.Message);
};
return this.RedirectToAction("Index");
}
[HttpPost]
public virtual ActionResult ManageTasks(ManageTaskModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
try
{ //User has seleced the user that they want to see Tasks for
if (model.selectedUser != null && model.newUser==null)
{
model.assignedPSUsers = Orchestrator.GetAssignedPSUsers();
model.FcvsTaskList = Orchestrator.GetTasksForAssignedPSUser(model.selectedUser);
return AssignTask(model);
}
}
catch (Exception e)
{
ModelState.AddModelError("ErrorMsg", e.Message);
return View(model);
}
return this.RedirectToAction("Index");
}
[HttpGet]
public virtual ActionResult AssignTask(ManageTaskModel model)
{
if (model.selectedUser != null && model.newUser == null)
{
**return View(model);** //returning the ManageTask instead of AssignTask View
}
return this.RedirectToAction("Index");
}
In your ManageTasks action you return AssignTask(model). This doesn't work, because the request context still remembers that the user actually called ManageTasks. That's why it returns the view for ManageTasks.
The right way to do it is like that:
return RedirectToAction("AssignTask", model); // remember to pass the model here
You can see that if you put this line in AssignTask:
HttpContext.Request.Path
If you access it from ManageTasks using return AssignTask(model), the value will be "/YourController/ManageTasks".
If you either call this action directly from browser or with RedirectToAction the value will be "/YourController/AssignTask".
you can't redirect that way. instead of return AssignTask you need
return RedirectToAction("AssignTask");
and pass an id or something there. you will need to recreate the model in your AssignTask method

MVC how to return a view after an IF statement

My page has a search box which for has a few radio buttons. Depending on which radio button is selected will depend on which view is shown.
However, I don't know how to return the View.
My code is
public ActionResult Index(string jobType)
{
if (jobType.ToLower() == "this")
CandidateResults();
else
JobResults();
}
private ActionResult CandidateResults()
{
var model = //logic
return View(model);
}
private ActionResult JobResults()
{
var model = //logic
return View(model);
}
But this displays nothing on screen (a white page). This makes sense but I don't want to return Index, I want to return a new page (called either JobResults or Candidates) and create a View for both of these new pages but when I right click in my methods (JobResults() or Candidates()) I don't get the option to Add View.
At this stage I'm lost, can any one please give advice.
Either return the view from Index or redirect to CandidateResults or JobResults actions.
public ActionResult Index(string jobType)
{
if (jobType.ToLower() == "this")
return CandidateResults();
else
return JobResults();
}
private ActionResult CandidateResults()
{
var model = //logic
return View(model);
}
private ActionResult JobResults()
{
var model = //logic
return View(model);
}
Try this
public ActionResult Index(string jobType)
{
return (jobType.ToLower() == "this") ?
RedirectToAction("CandidateResults") :
RedirectToAction("JobResults");
}
private ActionResult CandidateResults()
{
var model = //logic
return View(model);
}
private ActionResult JobResults()
{
var model = //logic
return View(model);
}
In your private methods you have to specify the actual view you want to display.
public ActionResult Index(string jobType)
{
if (jobType.ToLower() == "this")
CandidateResults();
else
JobResults();
}
private ActionResult CandidateResults()
{
var model = //logic
return View("CandidateResults", model);
}
private ActionResult JobResults()
{
var model = //logic
return View("JobResults", model);
}
This happens because of the way the view engine works. The action name for the current request is always Index when the index function is called. Even if you call another method, the view engine will use the name of the current action and not the name of the currently executing function.
Just you need to redirect the user to proper controller method and that method will return its View as below:
public ActionResult Index(string jobType)
{
if (jobType.ToLower() == "this")
return RedirectToAction("CandidateResults","ControllerName");
else
return RedirectToAction("JobResults","ControllerName");
}
public ActionResult Index(string jobType)
{
if (jobType.ToLower() == "this")
return RedirectToAction("CandidateResults");
return RedirectToAction("JobResults");
}
private ActionResult CandidateResults()
{
var model = //logic
return View(model);
}
private ActionResult JobResults()
{
var model = //logic
return View(model);
}

Categories

Resources