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

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"]);

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?

ASP.Net MVC Partial View Model Binding

I'm very new to MVC and I am looking to put a list of links on the main layout(master page) based on database table. I'm sure I read before that you shouldn't try to load models on the master page but use Partial Views instead (correct me if I'm wrong).
I've looked on Google and on other questions here but they only seem to talk about passing data from a main view to a partial view via ViewBag but I think I just want to add a partial view that I can add to the master page.
Can someone please tell me how to create a partial view I can add to master page so its used on every page and be able to load the list of links required i.e. by binding IEnumerable model to Partial View?
Try using ChildActionExtensions.Action
In your layout:
#Html.Action("MyAction", "MyController")
Controller:
public ActionResult MyAction()
{
var list = // get your list values
return PartialView("MyViewName", list);
}
Then just create your partial view:
#model IEnumerable<WhateverType>
#* View goodness *#
You can use this to bind whatever model you need to your partial view and if you use the Action helper in your Layout.cshtml it'll be rendered on every page.

Passing parameters in partial Views - MVC3/Razor

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.

declare label in mvc

I have a label called "test" in my Index.asp page in my MVC view folder. I want to be able to change the value of it in my controller class. Can someone explain how it can be achieved?
Your help is appreciated!
Quickest way, but there are better options depending on what you want to do:
CONTROLLER
public ActionResult Index()
{
ViewData["testValue"] = "new label value";
return View();
}
INDEX VIEW - ACCESS THE VALUE LIKE THIS:
<label for="test"><%= ViewData["testValue"] %></label>
If you use form view models, you can set the display name in the class, and then use the html helpers to generate your form items, which will automatically create your labels based on what you have set them to be.

ASP.NET MVC partial view that updates without controller

I have a partial view that shows a list of Categories. I'd like to put that partial view on any page, but I'd like to have it to call to the service and get a list of categories by itself without me having to do that in every controller action. Something like webforms in which you can put a code-behind on it.
For eg.
Actions
public ActionResult Index()
{
JobListViewModel model = new JobListViewModel();
model.Categories= jobService.GetCategories();
return View(model);
}
public ActionResult Details(int id)
{
Job job = jobService.GetJob(id);
return View(job);
}
I created a partial that will take the model.Categories model and display a list. As you can see, the Index page will work fine, but I do not want to call it again in the Details page. Is there a way to make my partialview call to the GetCategories() service by itself?
Use Html.RenderAction - that gives the partial view its own controller action.
You should also mark you partial action with the attribute [ChildActionOnly].
DVark,
As noted in the accepted answer, for your scenario, RenderAction is the most appropriate.
I thought I'd link a little article that distils my thinking on the topic (i.e. when to use RenderPartial vs RenderAction):
http://cbertolasio.wordpress.com/2010/09/21/mvc-html-renderaction-vs-html-renderpartial/
hope it helps
[edit] - as an aside. a year or so ago, i got myself into a few scrapes by not appreciating the power of RenderAction, in favour of RenderPartial. as a result, i had littered the shared view space with lots of partialviews in order to access them from a variety of sources. the moral of the story: know your 'territory' before planting your flag.

Resources