I have a partialview in _Layout.cshtml that I only want to display for certain urls.
My first thought was in my partial I would use a string as the model #model String .
In the actionmethod that is called I would return this
return PartialView("_MyPartial", new string{Request.FilePath});
In the partial I would have an if block wrapping my outer div that would check the model to see if the url it contained was the url that can display the partial.
I don't like this way because I would have to hardcode the url in if block check
#if( Model == "/Test/Home")
{
<div>
Just an example
</div>
}
What would be the best way to do this?
Thanks
You shouldn't need to use hard coded strings, even if you did the validation within your view like you initially intended.
You can use
Request.Url.AbsolutePath
to get your current url and
Url.Action("action", "controller")
to generate the inacceptable locations.
That said, I would keep your logic determining whether to show the partial view within your controller.
if(showPartialView)
return PartialView("_MyPartial");
else
return new EmptyResult();
Deciding actions based on the request is the responsibility of the Controller. Since the controller chooses the view, why not have it choose the partial as well? Figure out what, if any, partial you want in your controller, and pass it to the view on your view model.
Related
I have two controllers and two models (completely separate). I then have one view where I use an url.action to create a new record using the first controller and model. I then have another url.action to create a record for the other controller and model. My issue is that submitting one will submit the other. Is it bad practice to use a url.action to display the create view - because it works well when I only use one url.action. Or is it bad practice to have multiple. How else could this goal be achieved without having to use another view?
the view is pretty straight forward.
....
<div>
#*#Html.Action("Create", "MPost", new { TaskId = Model.TaskId, TaskTitle = Model.Title })*#
</div>
<div>
#Html.Action("Vote", "Vote", new { TaskId = Model.TaskId})
</div>
There is nothing wrong with using multiple Html.Action() calls within your Views, however you need to consider if that's the best option for you to use.
Determining What You Want To Do
Html.Action() is going to call the controller action when the View is initially rendered and it will perform whatever actions are in the View and then it may or may not return another View / Partial View.
You mentioned the following in your post :
My issue is that submitting one will submit the other. Is it bad
practice to use a url.action to display the create view - because it
works well when I only use one url.action.
This sounds like you actually want these to function as links and when they are clicked that you want to call them. If that is the case, you may be better off either creating a link that points to the appropriate action via the Url.Action() or Html.ActionLink() helpers :
<a href='Url.Action("Create", "MPost", new { TaskId = Model.TaskId, TaskTitle = Model.Title })'>Create New Post!</a>
These will perform basic GET requests to your Controller Action as you might expect. If you need to actually POST values, then you should consider using a <form> and submitting it :
<form action='#Url.Action("Vote", "Vote", new { TaskId = Model.TaskId})'>
<input type='submit' value='Cast Your Vote!' />
</form>
Generally, if you have two separate URLs that you might be posting values to, you should consider placing them each within their own <form> to avoid any confusion or possibly posting to the wrong area. Using links will work just fine as well, but keep in mind that they will redirect the user to the View that is returned from the Controller Action that you are hitting.
I think i know some of the basics of MVC but there's one thing which I don't understand yet.
In the view Create which is generated automatically when you set up your project, how is data sent to the controller? I'm used to seeing ActionLinks with parameters but here there's no actionLink so I can't understand how data travel from the view to the controller.
Could you explain it to me please?
as you know, in your view, the very first line (usually) tells the view, about the Model being used within this view. like:
#model Models.CarViewModel
lets suppose, you have a form on this view, and it is posted to some action called Edit. Then you must have your Edit action, expecting the parameter of type you used as model in your view. like:
[HttpPost]
public ActionResult(CarViewModel model)
{
//logic
}
This convention is known as Strongly Typed View. Suppose Then you have some textbox for a property Name of your model as:
#Html.TextBoxFor(x => x.Name)
when the form is posted to Edit Action, the variable model in parameter of Edit action will be holding the respective values. i.e, model.Name
I've seen how to use Partial Views from within a View and I understand how a model is passed from the View to a Partial View. What I don't grasp is how to include a Partial View inside of _Layout.cshtml so that I can pass it to the Partial View - or, how the Partial View itself can call a Controller Action to create a Model for use.
Specifically, I want to include a Partial View within _Layout.cshtml to display in the header of every page. The Partial View will display a custom setting in the User Profile. I can't think of any way to obtain the Model without making a Controller Action call - but how is this done in/for a Partial View from within _Layout.cshtml?
Is my only option of accessing a Controller Action to build my Partial View's required Model to use a jQuery call? Or is there another way?
The answer is by calling #Html.Action().
It sounds like you're on the right track.
The key is to try to not pass multiple models around, make models complicated, or use the ViewBag needlessly.
Instead, any time you want information on every page, call an action from your _Layout.
For example, you might have a controller called PartialsController or SharedController.
public class PartialsController : Controller
{
[ChildActionOnly]
public ActionResult UserProfilePartial()
{
UserProfileModel model = new UserProfileModel();
return PartialView("_UserProfile", model);
}
}
The ChildActionOnlyAttribute means that users cannot access the action directly. Only your code can call the action. You can also apply the attribute to the controller so that it affects all actions automatically.
Now call the action from your view (_Layout).
#Html.Action("UserProfilePartial", "Partials")
I don't know if I'm doing this wrong, but here's my problem. I need to pass a View's data to another Controller/Action.
In my HTML Form, I have
#using (Html.BeginForm("Preprocess", "Item", FormMethod.Post))
{
...some html...
...loop for each item in Items collection
<button type="submit" name="itemInfo" value="#Model.someValue">Submit</submit>
}
I receive the Form's data on my Item/Preprocess Action (strongly-typed view). However, I need to pass this to a 'central processor' that process the data depending on a certain flag. How do I pass the values I receive on this controller to another controller? I'm a beginner in MVC and I'm not even sure if this is the right way to do it.
Basically, I have three HTML forms similar from the above form, but with different Controllers and Views. I need them to call a single central master-Controller/Action and of course pass the data from the Form to the master Controller via HTTPPOST. Of course, each Controller has to format its own data to a class the master Controller can accept. What should I put in place of Return View() or RedirectToAction(...)?
Can you guys suggest a way to do this?
Or maybe you guys can suggest another way. It may not follow what I want, but basically my requirement is a central Controller/Action (or some other Centralized code) can receive the data and do operations depending on a value
You can do this:
return RedirectToAction("SomeAction", "SomeController",new { id=someString} );
Not sure I follow exactly what you are trying to do here. But from your description it does sound like your design is wrong.
If you are trying to perform some common set of actions once data is received and processed by your controllers then you should promote your "central controller" to be some kind of service class that can be accessed by all three controllers. The service class could return a ActionResult if necessary:
return new ViewResult { ViewName = "MyForm" };
However if you want to intercept the data and perform some common actions you could write a custom ActionFilter to do whatever your central controller is doing and leave the other controllers clean.
I am using PartialView object for all the CRUD operations in my project.
I would like to return from more than one action (Create/Read/Edit) always the same PartialView that I have previously binded to the correct model object.
How can I achieve this?
Thanks
You can specify, which (Partial)View you want to return from an action like this:
return PartialView("ViewName", viewModel);
If you want to use the partial view from different controllers, then put it in the Views/Shared folder.
Just add this to the end of every action that you want the partial returned from
return PartialView("PartialViewName", yourModelName)