Passing parameters in partial Views - MVC3/Razor - asp.net-mvc

How can I pass parameters to a partial view in MVC3 (razor). I replaced a regular View page with a Partial View in my MVC project. For a regular View page, I passed parameters like
public ActionResult MeanQ(int id)
{
Access access= db.Access.Find(id);
return View(access);
}
Now since I changed the view to a partial view, I have the following code instead:
public ActionResult MeanQ(int id)
{
Access access= db.Access.Find(id);
return PartialView("_MeanQPartial");
}
but do not know how I can still pass the parameter 'id' to make it work like before. Please help. For what its worth, the View or the partial View , both are triggered by a link and displayed in a Jquery Modal Dialog box.

Try this
return PartialView("PartialViewName", access);

Simply give it as 2nd parameter. PartialView method has 4 overloads and this includes one with two parameters PartialView(string viewName, object model)
public ActionResult MeanQ(int id)
{
Access access= db.Access.Find(id);
return PartialView("_MeanQPartial", access);
}
For what its worth, the View or the partial View , both are triggered by a link and displayed in a Jquery Modal Dialog box.
View would return an entire page using your layout. PartialView only returns the HTML from your partial. For a modal dialog, the partial is enough. No need to retrieve a complete page.

Related

MVC RenderAction return only renders partial part

I have this contact form which I want to use on two pages (two views) in MVC 5.x (razor viewengine) So I have put the form in an partialview called _Contact and I have read that RenderAction is the best approach if you do not have the required data for the partialview in your model and if it is more standalone (seperate from the rest of the view)
So I call it like this:
#{ Html.RenderAction("SendMail", "Uk");}
My Uk controller has these two methods:
[HttpGet]
public PartialViewResult SendMail()
{
return PartialView("_Contact");
}
[HttpPost]
public PartialViewResult SendMail(FormCollection fc)
{
// send mail using values out of the form (sorry did not feel like building a complete model for it
ViewBag.Succeed = true;
// if smtpclient could not reach server etc. it returns false
return PartialView("_Contact");
}
it all works, but the PartialView is only rendered, not on the placeholder where i call the RenderAction. It works all great, but after the post it just displays the partial view and not the "parent view" and the shared layout view etc. I hope that I made myself clear. Please let me know if I need to add more info.
This is the BeginForm from my shared view:
using (Html.BeginForm("SendMail", "Uk", FormMethod.Post))
It will not work as expected for your current code, because when you post the form, it returns Partial View not complete View. If you want to get only partial view then you have to submit your form via Ajax.
In ajax's success handler you will get HTML of your partial view and that you can put in a DIV tag of partial view container.
This link will give you a better idea about Posting Partial View via Ajax.
ASP.NET MVC Partial view ajax post?

How to pass CultureInfo to partialView for ajax call?

I have a viewpage and a partialview on that. For default request-when page is called- CultureInfo is set in base class of viewPage. I have created this custom base class which initialize the culture depending on a cookie and does some other stuff as well. There is a search button on the view page which updates a partial view on the page with Ajax call (jquery Ajax call). There is separate action for this partial view update call which returns partial view. The problem is partial view do not have any base class and when resources are displayed on partial view they are displayed for default culture and not the current culture. What would be best approach to set cultureInfo when partial view is called from jQuery Ajax call?
I have this mvc application as part (area) of bigger existing application so I can not touch session_start.
I'm not sure why you need to pass the culture around, can't you get it directly from your PartialView using a helper method?
#YourApp.CultureHelper.GetCurrentCulture()
which is just calling
public static string GetCurrentCulture()
{
return Thread.CurrentThread.CurrentCulture.Name;
}
If you are doing any work with internationalization and ASP.NET MVC I highly recommend reading through these two excellent posts by Nadeem Afana
ASP.NET MVC 3 Internationalization
ASP.NET MVC 3 Internationalization - Part 2 (NerdDinner)
Very well written and explains everything you need to know about internationalization.
You can send the culture info to your partial view as model with string array.
I guess you have handled the ajax call so I am just gonna show you the way of passing the value. The below one is your controller :
public ActionResult Index() {
return PartialViewResult("samplePartial", new string[] { "en" });
}
And this is your partial view :
#model string[]
#if(Model[0] == "en") {
#:Culture is "en"
} else {
#Culture is different than "en"
}
Also this can be also rendered inside your view as indicated below :
#Html.Partial("samplePartial", new string[] { "en" })

ASP.NET MVC calling partial view from another partial view

I have a page that uses a couple of partial views. My first partial view has some options that when the user selects, and presses a button, it gets data from a database and renders the other partial view on that page.
What is the best way to go about this? I've not really done much in MVC before.
Thanks.
In MVC, views are only concerned with rendering models from your controller. You need to set up a controller action to accept the view options and then render the second partial. Roughly...
[HttpGet]
public ActionResult Foo()
{
return View(); // Foo.aspx is not given a model, so don't show second partial
}
[HttpPost]
public ActionResult Foo(bool option1, string option2)
{
var data = repository.GetData(option1, option2);
var model = new FooModel(data);
return View(model); // Foo.aspx is given a model, so show second partial
}

ASP.NET MVC Model contents disappears randomly

Update following initial comments
The model has an object in it called 'Account', of which there is an int propety (Account.AccountID)
ViewB has a form which collects some additional information - but also has a textbox which is populated with Model.Account.AccountID.
When I submit ViewB however, Model.Account becomes null.
Its probably easier to show a simplified version of what I have before I explain the issue:
[HttpGet]
public ActionResult ViewA()
{
return View(new BlahModel());
}
[HttpPost]
public ActionResult ViewA(BlahModel model)
{
if(there_was_a_problem)
return View("ViewA", model);
else
return View("ViewB", model);
}
// have tried both httppost, httpget and no attribute here
public ActionResult ViewB(BlahModel model)
{
return View(model);
}
I load up ViewA via a GET, fill in the strongly-typed form and submit - then the following View (either ViewA again or ViewB if the request had no problems) is fine ... it has full access to the whole model and can display the properties within it.
The problem is that if I then submit a form in ViewB (which posts to the ActionResult ViewB) - the model suddenly has null properties throughout, even though its using the same model - and prior to the post has picked up all the values sucessfully.
Any ideas?
Many thanks
Most likely - ViewB view does not render enough and model binder can't find values to bind.
Action argument is binded from form values. It does not matter if You pass model to view correctly. Rendering things out is what matters (or passing through query string/cookies).
Complementing Arnis L.'s answer, you can use a tool like Firebug to check that your model's parameters (or any parameters at all) are being sent along the request.

how to take the html helper controls in the master page?

I want to take two html helpers textboxes on my master page please tell me what shall I do.I want to pass the value in the textboxes from the controller class.Please tell me how can I do that.
Thank You
Ritz
Create a user control instead that contains the text boxes. Then on your master page, include the user control. That way in your controller you're still able to pass values to it through ViewData.
Assuming ASP.NET MVC:
In your controller you can do the following:
public ActionResult Index() {
ViewData["YourText"] = "Hello!";
return View();
}
Your page:
<%= Html.TextBox("NameOfTextBox", ViewData["YourText"]);

Resources