ASP.NET mvc display int number in LabelFor - c#

Why it's displaying data, but not the value 100
public ActionResult Index()
{
ViewBag.id = 100;
return View();
}
Index.cshtml
#model TestForm.Models.Data
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
#using(#Html.BeginForm())
{
#Html.LabelFor(model=>model.data,((int)#ViewBag.id))
}

Remove the '#' if you want to display 100 that you stuffed into a ViewBag
Viewbag:
ViewBag.id = "100";
Html:
#model TestForm.Models.Data
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
#using(#Html.BeginForm())
{
#Html.LabelFor(model=>model.data,((string /*LabelFor produces string lableText */)ViewBag.id))
}
Or:
#Html.Label("id", (int)ViewBag.id)

Instead of the LabelFor, you could always do the following:
<label>#Html.Raw(ViewBag.Id)</label>
LabelFor is really reserved for Model properties since the model is able to pass Data Types and Data Annotations like Email, Password.
By doing it the more raw HTML way, you can style is appropriately like you would any other HTML element.

If you want to show 100 on label text, then why are you using Html.LabelFor? The first parameter of LabelFor shows the text of the property (or the DisplayAttribute inside your model), which in your case is data.
Use Html.Label instead, like this
#Html.Label("id", (int)ViewBag.id)
Or, in a simply way, use the html version:
<label>ViewBag.id</label>

Related

How to add two variables in razor view

I am trying to add 2 variable in my razor view. I have assigned some fixed values to Value1 and Value2 in web.config file. But instead of adding values its concatenating (Instead of add 10+10=20 I am getting 1010) with below code.
#model Myproject.Models.Cost
#{
ViewBag.Title = "About";
var addvalue1 = ConfigurationManager.AppSettings["Value1"];
var addvalue2 = ConfigurationManager.AppSettings["Value2"];
var total = addvalue1 + addvalue2;
}
<h2>#ViewBag.Title.</h2>
<h3>#ViewBag.Message</h3>
#using (Html.BeginForm())
{
<p>Your total is : #total</p>
}

How to set tag attribute with value from model

I have img tag in the view and I want to set the src attribute of that img with value from model object like that:
#model myObject
#{
ViewBag.Title = "title";
}
<img src="#Model.mysrc" alt="error" />
but it's not work and I try to use jquery but not work too:
$('img').attr("src", "#Model.mysrc");
the problem was because of the string in the #Model.mysrc = "~/images/photo.jpg"
it is tilde, i remove tilde and it work right,

how to strongly type an action from a view

I'm very new to using the MVC system and I would like to know how to "strongly-type" the controller as opposed to using a string in an action-link residing in a view. I put strongly typed in quotes because I am still a bit unsure as to its usage. Would be correct in saying that I have strongly typed the model at the top of the next code=line?
Here's my view:
#model HtmlEditModel
<title> Input Yout Html </title>
#Html.BeginForm(
#Html.TextAreaFor(m => m.HtmlInput))
<input type ="submit" />
#Html.ActionLink("Back to List page",
Here's my Action:
[HttpGet]
public virtual ActionResult HtmlEdit(
ActionMode mode,
int? id = null,
string returnUrl = null)
{
return base.DataGet<HtmlEditModel, Html>(mode, ID, db.Htmls, returnUrl);
}
#using(Html.BeginForm(action, controller)){ /*form fields go here*/}
I couldn't quite understand your comment, but after looking at your code I think you meant to say how to "remove" the ActionLink and bind the form to a specific action in a controller. Is this what you were asking for?

Can I use IEnumerable<> inside a View?

In _Layout.cshtml
#model DynaPortalMVC.Models.Page
#using System.Linq
<ul>
#IEnumerable<model.Page> pages = model.Where(x=>x.CompanyID == 1);
#foreach (var item in pages)
{
<li>item.Title</li>
}
</ul>
In view iam trying to filter the model object called 'page' and get a list of pages whose id is 1. I need to iterate through this to show the menu.
Code inside Controller
public ActionResult Menu(string PageName)
{
//
return View(PageName, db.Pages);
}
Please tell me, how to filter out this model object to a list? I get errors on using IEnumerable.
Solved
I changed the model object to IEnumerable in view page.
#model IEnumerable<DynaPortalMVC.Models.Page>
You can skip assigning the result of your query into an IEnumerable variable unless you will use it somewhere else on the page. So you can just do this:
#model DynaPortalMVC.Models.Page
#using System.Linq
<ul>
#foreach (var item in model.Where(x=>x.CompanyID == 1))
{
<li>#item.Title</li>
}
</ul>
You need
# {IEnumerable<model.Page> pages = Model.Where(x=>x.CompanyID == 1);}
#model IEnumerable<DynaPortalMVC.Models.Page>
#{
ViewBag.Title = "Page";
}

Model Binding with variable number of items in List<T>

I have a model that can have a variable amount of items in a List<T>
In my view I then have the following:
#using (Html.BeginForm())
{
int count = Model.Data.Filters.Count;
for(int i = 0; i < count; i++)
{
<div>
#Html.TextBox("filtervalue" + i)
#Html.DropDownList("filteroptions"+i,Model.Data.Filters[i].FilterOptions)
</div>
}
#Html.Hidden("LinkID", Url.RequestContext.RouteData.Values["id"])
}
Is there a way in my controller so I can set up the POST action method to bind to a model with variable items in it?
Also how would I construct the model to cope with this?
Thanks
You coud use editor templates, it will be much easier:
#using (Html.BeginForm())
{
#Html.EditorFor(x => x.Data.Filters)
#Html.Hidden("LinkID", Url.RequestContext.RouteData.Values["id"])
}
and inside the editor template (~/View/Shared/EditorTemplates/FilterModel.cshtml) which will be automatically rendered for each element of the Model.Data.Filters collection:
#model FilterModel
<div>
#Html.TextBoxFor(x => x.FilterValue)
#Html.DropDownListFor(x => x.SelectedFilterOption, Model.FilterOptions)
</div>
Now your POST controller action will simply look like this:
[HttpPost]
public ActionResult Foo(SomeViewModel model)
{
// model.Data.Filters will be properly bound here
...
}
Thanks to editor templates you no longer have to write any foreach loops in your views or worry about how to name your input fields, invent some indexed, ... so that the default model binder recognizes them on postback.

Categories

Resources