I known if I have something like /controller/action/{id} I can access id as a function parameter. But how would I access it via the view without using the viewbag?
You can pass that function parameter to the View as part of a model. The model can be staticly or dynamically typed. The example code below demonstrates how to pass the value as a property on a dynamic model.
public ActionResult Edit(string id)
{
dynamic model = new System.Dynamic.ExpandoObject();
model.Id = id;
return View(model);
}
You would access this value in the view as follows:
#Model.Id
You can parse the URL to get the ID when in the view:
var id = Request.Url.LocalPath.SubString(LastIndexOf("/",Request.Url.LocalPath)+1);
It will be put directly into the ViewBag.id or you can access it via ViewData["id"]. hey seem like the same object
Related
I am trying to pass a modelclass store to a RenderPartial. The goal for the renderpartial is to change/set values on this (store)model. I have been trying like this:
#{ Html.RenderPartial("test", new store(){Output=""}); }
#{ Html.RenderPartial("test", new store(){Output2=""}); }
public class store
{
public string Output { get; set; }
public string Output2 { get; set; }
}
the Partial 'test' has to change the Output properties. Is it uberhaupt possible and if yes how to do this? The renderpartial contains a javascript to calculate the value of the properties.
RenderPartial is meant to get either none of the data from the parent or a part of the model.
The overloads at:
http://msdn.microsoft.com/en-us/library/system.web.mvc.html.renderpartialextensions.renderpartial(v=vs.108).aspx
Tell you object isn't a custom object but meant to be part of the Model, ex Model.Customers
Pass the required value from your model to the partial and let the partial create it's own objects.
If you really want to pass it to the partial then create a new view model for your parent view and set the Output property in your viewmodel and pass it to the partial.
Note also that the partial gets it's own copy of the data and cannot update the parents copy so that may defeat what you actually want here.
If you need some other calculate data do it in your controller prior to passing it to the view if possible.
#store st = new store(){Output="", Output2=""};
#{ Html.RenderPartial("test", new RouteValueDictionary {{"output", st.Output}, {"output2", st.Output2}}); }
How do I pass a whole model via html.actionlink or using any other method except form submission? Is there any way or tips for it?
Though it's not advisable in complex cases, you can still do that!
public class QueryViewModel
{
public string Search { get; set; }
public string Category { get; set; }
public int Page { get; set; }
}
// just for testing
#{
var queryViewModel = new QueryViewModel
{
Search = "routing",
Category = "mvc",
Page = 23
};
}
#Html.ActionLink("Looking for something", "SearchAction", "SearchController"
queryViewModel, null);
This will generate an action link with href like this,
/SearchController/SearchAction?Search=routing&Category=mvc&Page=23
Here will be your action,
public ViewResult SearchAction(QueryViewModel query)
{
...
}
No, you cannot pass entire complex objects with links or forms. You have a couple of possible approaches that you could take:
Include each individual property of the object as query string parameters (or input fields if you are using a form) so that the default model binder is able to reconstruct the object back in the controller action
Pass only an id as query string parameter (or input field if you are using a form) and have the controller action use this id to retrieve the actual object from some data store
Use session
You could use javascript to detect a click on the link, serialize the form (or whatever data you want to pass) and append it to your request parameters. This should achieve what you're looking to achieve...
I have a person class that has an index file that lists each person.
An Html.ActionLink points to another controller for notes on the person.
One person, many notes, so I want to pass the PersonID as a param and insert it into the new note form. The parameter is not the NoteID, ie the key
#Html.ActionLink("Note", "Create", "Note", new { id = item.PersonID }, null)
The PersonID is in the url that passes to the note form.
How do I get the PersonID into the Note form?
Many thanks,
Harry
In your Create action method in the Note controller, you will receive the ID as a paramter to the action method if you include a parameter for it in the function:
public ActionResult Create(string id)
{
}
From there it is up to you to pass the id into the view you are using to construct the Create Note page. If you have a Note model object and one of the properties is the PersonID, you might want to new up a new Note object, set the PersonID to the value passed in the parameter, and then pass the new Note object to the view page as the model:
return View(newNote);
Hope that is what you are looking for.
You can knockout it using the same name as u declared ID
Or.. just handle it on another Controller using
the same name. ID:
[HttpGet]
public ViewResult Create(int id)
{
return View(id); <--
}
Then with razor-engine it comes with #Model
I have this post method:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Invitations(SuperInvitationsEditModel model)
{
...
var newmodel = new SuperInvitationsEditModel();
if (hasErrors)
{
SuperInvitationsErrorModel newErrorModel = new SuperInvitationsErrorModel();
newErrorModel.Errors = model.Errors;
return View(newErrorModel);
}
return View(newmodel);
}
When this code in the if(hasErrors) executes I get this error.
The model item passed into the dictionary is of type 'MyProject.Models.SuperInvitationsErrorModel', but this dictionary requires a model item of type 'MyProject.Models.SuperInvitationsEditModel'.
I thought I can do this since the return value of the method is a generic ActionResult. Can anyone tell me why is this not working?
because your current view is strongly typed. change the code as
return View("yourviewname",newErrorModel);
It has nothing to do with casting ViewResult to ActionResult. The problem is, that you have strongly typed view that expects the model of type SuperInvitationsEditModel (see #model on the top of Invitations.cshtml), but you are passing the model of type SuperInvitationsErrorModel to it.
You should merge the two view model classes (SuperInvitationsEditModel and SuperInvitationsErrorModel) into one, or create a standalone view for each of them.
I am just new to MVC.
when we use "#Html.EditorFor" in razor view, it generates textbox.
My requirement is that I need to supply some value from viewbag or session to user's in that textbox?
Is it possible and if yes how can i do?
OR
What are the alternatives?
In your action method in the controller, pre-load a model with some data:
public ActionResult Index()
{
MyModel model = new MyModel();
model.FirstName = "Bob";
model.LastName = "Hoskins";
return View(model);
}
Then make your View strongly typed. These pre-set values should now appear on your view. You probably want to populate them from a service layer or resource file, rather than have them as hardcoded strings like my example.