How to add two variables in razor view - c#

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>
}

Related

How display two columns in Mvc WebGrid with single object

I am new to MVC and I want to display name of a user in two columns in a WebGrid as display below.
Currently the web grid display data in below format.
I am binding my WebGrid with List<string>, list contains username.
My Controller
public ActionResult Index()
{
List<string> userList = new List<string>();
userList.Add("User1");
userList.Add("User2");
userList.Add("User3");
userList.Add("User4");
return View(userList);
}
My Cshtml
#using (Html.BeginForm())
{
var grid = new WebGrid(Model);
#grid.GetHtml(columns: grid.Columns(grid.Column(format: (item) => item)));
}
Your desired output is not the right use-case for a webgrid control. Here is the table generation logic for mvc razor view using simple for loop:
#model List<string>
<table >
#{
for (int i = 1; i <= Model.Count; i++)
{
if (i % 2 != 0)
{
#:<tr >
}
<td style="border:solid;">
#Model[i - 1]
</td>
if (i % 2 == 0)
{
#:</tr>
}
}
}
</table>
I've added a css style style="border:solid;" just to show border of all the table cells. You can customize it as per your needs.
This code creates below table:

ASP.NET mvc display int number in LabelFor

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>

Model MVC IEnumerable without Iteration

Rookie Question. So I have a IEnumerable Model And as you can see my question is simple. I just want to get the Value of the First item in the model Without iterating through the whole model.
As this is only one instance to happen. I don't want to itterate from the model just to get 1 Entity from it and I wouldn't also want to rely on the ViewBag or ViewData for this.
Is this possible?
Client = Model.FirstOrDefault().ClientID
Object reference not set to an instance of an object.
I also tried select()
#model IEnumerable<RMQGrainsFinalCement.ModelsCorn.POCorn>
#{
ViewBag.Title = "POIndex";
}
<h2>PO Index</h2>
#Html.ActionLink("Add PO", "Create", "POCorns", new {ClientID = Model.FirstOrDefault().ClientID },null)
To mitigate unexpected errors, do something like this in your view:
#{
var clientId = 0;
if (Model.Any())
{
clientId = Model.First().ClientId;
}
}
#Html.ActionLink("Add PO", "Create", "POCorns", new {ClientID = clientID },null)

Can i use #using(null) to isolate the variables in the View

In some cases, I use the same variable names in the one View, for this i use #using statement with null, like this:
<div>
#using (null) {
var url = "http://example.com/";
example.com
}
#using (null) {
var url = "http://simple.com";
simple.com
}
</div>
Wanted to clarify whether I'm doing well?

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";
}

Categories

Resources