declare label in mvc - asp.net-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.

Related

MVC Create method data flow

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

Display partialview based on a url

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.

getting dropdown's text in Post action method

I have a Razor view in my asp.net MVC3 application with a dropdownlist like this:
#Html.DropDownListFor(model => model.Account.AccountType, new SelectList(Model.AccountTypes, "AccountTypeCode", "Abbreviation"))
This dropdown is inside a form. When form is posted to action method and viewmodel is filled because of model binding, It get the value(AccountTypeCode) and not the text "Abbreviation" property of dropdownlist. I want to get both of these. how can I get these in post action method.
Please suggest.
If you need more than one property of an object as a value for a dropdown, the easiest way is to create a combination of these in a partial class
EF 4, how to add partial classes this quesion should help you. You will be able to combine values under one property that you will provide to your dropdown helper.
If you don't want to use partial classes I would advise creating your own helper, that will be a lot easier than trying to use something that does not fit your needs. You can do something like :
#helper CustomDropdown(string name, IEnumerable<AccountTypes> valueList)
{
<select>
#foreach (var item in valueList)
{
<option value="#item.Abbreviation #AccountTypeCode">#item.Abbreviation</option>
}
</select>
}
Google "Creating an Inline HTML Helper" to get some valuable resources on that topic
I struggled with this recently, and the best I could do was:
ViewBag.Message = myModel.myProperty.ToString().
in the controller action. Assuming myProperty is AccountType.
Then in my view I just did
#ViewBag.Message
The next problem I encountered is that it spit out the exact text, without spacing. I had to use a helper to add spacing (but it was based on each word being capitalized, so "ThisIsSomeText" would show up as "This Is Some Text".

Asp.net MVC, after using jeditable (Edit in place)

Ok, i can use jeditable to edit-in-place some content on a page and the content will be saved to a database. But whats the best way to re get that text-content from db to show into a place holder?
p id="paraNo34" class="editable"
-->What i will write here so that it will get content from a
db's table: [Content], where id=="paraNo34".
/p
The problem is if i will use some hard coded text like
p id="paraNo34" class="editable"
-->Some text here
/p
I will able to edit-in-place using jeditable but when i will refresh page it will show the same "Some text here" as its not getting data from db.
Your pseudocode implies that you want the view to be responsible for fetching the required data, which is an anti-pattern in MVC. You need to retrieve the text in your controllers action and pass it to the view, either using ViewData or a custom view model, e.g.:
public ActionResult Index(string id)
{
// call some method to fetch data from db
ViewData["ID"] = id;
ViewData["Content"] = content;
return View();
}
And the view looks something like:
<p id='<%= ViewData["ID"] %>' class="editable">
<%= Html.Encode(ViewData["Content"]) %>
</p>
A better approach would be to create a strong-typed view model (Stephen Walther has a blog post about view models here), but the above example should illustrate how data can be passed from the controller to the view.

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