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?
Related
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>
}
I am trying to create a custom url which is within a loop so that through each loop the produced url is different. The code looks like this:
#foreach (var car in Model)
{
<div class="container">
#{string country = Html.DisplayFor(modelCar => car.Country).ToString()}
#{string mapUrl = "https://maps.googleapis.com/maps/api/staticmap?zoom=14&size=150x120&key=[insertkey]¢er="
+ country}
<img src=#mapUrl />
</div>
}
The problem is the line where I define mapUrlrenders an error. What is the problem with this code?
Thanks in advance.
I think you are simply missing the semi columns for the variables and quotes for the image src tag.
Also i am not sure why you are calling DisplayFor to get the country variable name! DisplayFor returns an MvcHtml string and is usually used to get the encoded html markup to render to browser. Unless you have some crazy overloading of that (why would you do that on DisplayFor ? Use a simply helper method), do not misuse the DisplayFor helper
This should work.
#foreach (var car in Model)
{
<div class="container">
#{ var country = car.Country; } // i assume you just need the Country
#{ var mapUrl = "https://maps.googleapis.com/maps/api/staticmap?zoom=14&size=150x120&key=[insertkey]¢er=" + country; }
<img src="#mapUrl" />
</div>
}
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>
I want to access the value of button in controller and then pass it to the view as well.
But the value of "str" passed from view to controller is null.
Index.chtml
#{
var str = "Shoes";
}
<a href="~/Home/Products_By_Category/#str" target="_parent">
<input type="button" value="Shoes" class="btn"/>
</a>
/////////////////////////
<pre lang="c#">
public ActionResult Products_By_Category(string s)
{
ViewBag.category = s;
return View();
}
Have the same name s for your input also.
Use ViewBag.category in the client side. Since ViewBag is a dynamic entity.
Are you using form submit in which case MVC will automatically fill the value of the input for you.
Now I'm using
Index.cshtml
#{
var str="Shoes";
}
<form action="~/Home/Products_By_Category/#str" method="post">
<input type="submit" value="Shoes" class="btn"/>
</form>
/////////////
public ActionResult Products_By_Category(string s)
{
var context = new Shoe_StoreDBEntities();
var q = from p in context.Products
join c in context.Categories on p.CategoryId equals c.Id
where c.Name.Equals(s)
select new { p, c };
}
but still the value in "s" is still null
First. The controller passes data to the view. There is no other way around. It's because the basic nature of a webapplication.
Basically: Request -> controller -> select and render a view.
The view itself is not a known concept in the client browser. That is simple html/css/js.
Your view should looks like this:
#{
var str = "Shoes";
}
#using (Html.BeginForm("Products_By_Category"))
{
<input type="submit" name="s" id="s" value="#(str)"/>
}
Some note:
1) If you use a variable inside an html element attribute you have to bracket it.
2) You should use the builtin html helper whenever it's possible (beginform)
3) This example only work if you click the submit button to postback the data. If there is other submit button or initiate the postback from js, the button data is not included into the formdata. You should use a hidden field to store the str value and not dependent from the label of the button:
#{
var str = "Shoes";
}
#using (Html.BeginForm("Products_By_Category"))
{
<input type="hidden" id="s" name="s" value="#(str)"/>
<input type="submit" value="Do postback"/>
}
In my page i have
#using (Html.BeginForm("list", "menu", FormMethod.Get))
{
<div>
Show categories:
#Html.DropDownList("groupName", (SelectList)ViewBag.groups)
<input id="Submit1" type="submit" value="Show" />
</div>
}
Regarding the option the user choose i generate a list and the Querystring in my address going to be like :
localhost/menu/list?groupName=controlpanel
My problem is when i use HtmlActionLink for example :
#Html.ActionLink("Title", "List", new { foo = item.foo})
What i got in result is result is :
localhost/menu/List?foo=123
instead of :
localhost/menu/List?foo=123&groupName=controlpanel
Am i missing something ??
It is not a built in solution, even though it seems to address exactly what you are looking for:
ASP.NET MVC Build Url Based on Current Url
To use ActionLink, you need to include all of the parameters you want to appear in the query string. The easiest thing I think would be is to add a GroupName property on your model (or in the ViewBag like your other sample). Then you can do this:
#Html.ActionLink("Title", "List", new { foo = item.foo, groupName = Model.GroupName })