I'm still fairly new to MVC and Razor and I'm hoping you can help me out with something. I am able to use the following code to just print the line (using #reply.GetProperty("PropertyName") but when I try to assign it to a variable (test) the code fails. Why is this and how can I get around it? I really need to be able to assign it and use it further in some other code.
#foreach (var reply in CurrentPage.Children)
{
#reply.GetProperty("PropertyName")
#{
string test = reply.GetProperty("PropertyName");
}
}
In your View
#{
string test = string.Empty;
}
#foreach (var reply in CurrentPage.Children)
{
#reply.GetProperty("PropertyName")
test = reply.GetProperty("PropertyName");
}
Related
I am trying to read the value of a particular property in view from ViewModel. But I am unable. See the code below and look at the "check". "check is assignable at one place but then I am unable to read its value in the view.
#{string check;} //created a variable
#foreach (var item in #Model )
{
check = "yes"; //this is good
return;
}
#{if(check=="yes")} //this here show the error not recognizing the check.
or
#{
string check;
#foreach (var item in #Model )
{
check =item.FirstTime;
return;
}
if(check!=0) //this is unrecognized
}
I am new to MVC. I am trying for Cascading Dropdownlist in ASP.NET MVC 4. My problem is I want to access the selected countryid in #foreach loop
#Html.DropDownList("ddlCountry", new SelectList(#Model.Countries, "CountryId", "CountryName"))
$("#ddlCountry").change(function () {
#foreach (var item in #Model.States.Select(x=>x.countryId = ?))
{
}
});
How can I substitute countryId with the question mark above, so that I can filter the states
Thanks in Advance
Thanks
SRRIN
you can get the value in jquery by this
$("#ddlCountry option:selected").text();
As #Stephen said you cannot get the value of selected option using razor. Because the razor code is executed before page load.
A work around would be
$("#ddlCountry").change(function () {
var selectedVal= $("#ddlCountry option:selected").text();
var list = #Model.States.Tojson();
});
then use inArray for doing further operations
Thanks to all.
As Stephen and Nikitesh stated, javascript variable are not accessible inside razor scripts. So I succeeded with following approach.
var selectedCountryId = $("#ddlCountry").val();
var states = #Html.Raw(Json.Encode(Model.States));
for (var i = 0; i < states .length; i++) {
if (states [i].countryid == selectedCountryId ) {
... populate states here
}
}
Thanks
SRRIN
you can use dropdown selected value and pass in your query as:
#Html.DropDownList("ddlCountry", new SelectList(#Model.Countries,"CountryId", "CountryName"))
$("#ddlCountry").change(function () {
var countryId=parseInt($(this).val());
#foreach (var item in #Model.States.Select(x=>x.countryId = countryId))
{
}
});
Quite simple question. I have the following code
#Html.Raw(following.Description).ToString()
when this comes from database it has some markup in it (its a forum post but i want to show a snippet in the list without the markup
is there any way to remove this and replace this line or shall I just regex it from the controller?
Here is a utility class extension method that is able to strip tags from fragments without using Regex:
public static string StripTags(this string markup)
{
try
{
StringReader sr = new StringReader(markup);
XPathDocument doc;
using (XmlReader xr = XmlReader.Create(sr,
new XmlReaderSettings()
{
ConformanceLevel = ConformanceLevel.Fragment
// for multiple roots
}))
{
doc = new XPathDocument(xr);
}
return doc.CreateNavigator().Value; // .Value is similar to .InnerText of
// XmlDocument or JavaScript's innerText
}
catch
{
return string.Empty;
}
}
Hi i need to show a list of data using viewbag.but i am not able to do it.
Please Help me..
I tried this thing:
ICollection<Learner> list = new HobbyHomeService().FetchLearner();
ICollection<Person> personlist = new HobbyHomeService().FetchPerson(list);
ViewBag.data = personlist;
and inside view:
<td>#ViewBag.data.First().FirstName</td>
But this does not show up the value and gives error saying "Model.Person doesnot contain a defibition for First()"
In your view, you have to cast it back to the original type. Without the cast, it's just an object.
<td>#((ViewBag.data as ICollection<Person>).First().FirstName)</td>
ViewBag is a C# 4 dynamic type. Entities returned from it are also dynamic unless cast. However, extension methods like .First() and all the other Linq ones do not work with dynamics.
Edit - to address the comment:
If you want to display the whole list, it's as simple as this:
<ul>
#foreach (var person in ViewBag.data)
{
<li>#person.FirstName</li>
}
</ul>
Extension methods like .First() won't work, but this will.
To put it all together, this is what it should look like:
In the controller:
List<Fund> fundList = db.Funds.ToList();
ViewBag.Funds = fundList;
Then in the view:
#foreach (var item in ViewBag.Funds)
{
<span> #item.FundName </span>
}
simply using Viewbag data as IEnumerable<> list
#{
var getlist= ViewBag.Listdata as IEnumerable<myproject.models.listmodel>;
foreach (var item in getlist){ //using foreach
<span>item .name</span>
}
}
//---------or just write name inside the getlist
<span>getlist[0].name</span>
i had the same problem and i search and search .. but got no result.
so i put my brain in over drive. and i came up with the below solution.
try this in the View Page
at the head of the page add this code
#{
var Lst = ViewBag.data as IEnumerable<MyProject.Models.Person>;
}
to display the particular attribute use the below code
#Lst.FirstOrDefault().FirstName
in your case use below code.
<td>#Lst.FirstOrDefault().FirstName </td>
Hope this helps...
Use as variable to cast the Viewbag data to your desired class in view.
#{
IEnumerable<WebApplication1.Models.Person> personlist = ViewBag.data as
IEnumerable<WebApplication1.Models.Person>;
// You may need to write WebApplication.Models.Person where WebApplication.Models is
the namespace name where the Person class is defined. It is required so that view
can know about the class Person.
}
In view write this
<td>
#(personlist.FirstOrDefault().Name)
</td>
Just put a
List<Person>
into the ViewBag and in the View cast it back to List
This is what i did and It worked...
C#
ViewBag.DisplaylList = listData;
javascript
var dispalyList= #Html.Raw(Json.Encode(this.ViewBag.DisplaylList));
for(var i=0;i<dispalyList.length; i++){
var row = dispalyList[i];
..............
..............
}
//controller You can use this way
public ActionResult Index()
{
List<Fund> fundList = db.Funds.ToList();
ViewBag.Funds = fundList;
return View();
}
<--View ; You can use this way html-->
#foreach (var item in (List<Fund>)ViewBag.Funds)
{
<p>#item.firtname</p>
}
I had the problem that I wanted to use my ViewBag to send a list of elements through a RenderPartial as the object, and to this you have to do the cast first, I had to cast the ViewBag in the controller and in the View too.
In the Controller:
ViewBag.visitList = (List<CLIENTES_VIP_DB.VISITAS.VISITA>)
visitaRepo.ObtenerLista().Where(m => m.Id_Contacto == id).ToList()
In the View:
List<CLIENTES_VIP_DB.VISITAS.VISITA> VisitaList = (List<CLIENTES_VIP_DB.VISITAS.VISITA>)ViewBag.visitList ;
I have the following code:
string init = "yes";
string html = "";
foreach (var item in v.Details) {
if (item.Substring(0, 1) != " ")
{
if (init != "yes")
{
html += "</ul>";
}
html += "<p>" + item + "</p><ul>";
}
else
{
html += "<li>" + item.Substring(1) + "</li>";
}
}
The code is in my MVC controller and it creates a string called html. The thing is I don't think it should be in the controller. I tried to put this into the view and ended up with a huge mess that doesn't seem to work. Seems I am not very good at coding C within a razor view. I just saw a lot of syntax type errors and confusion between what's C and what's HTML.
Can anyone suggest how I could make this code work within a view. Here's what I used to have:
<ul>
#foreach (var item in Model.Details)
{
<li>#item</li>
}
</ul>
This worked but as you can see I now need more processing. Would it be better to take this out of the view and if so how could I do this. I'm really hoping for a view solution but I am confused about where the put the #'s and where to put the brackets.
Any experts at coding C inside or Razor out there?
The code is in my MVC controller and it creates a string called html.
The thing is I don't think it should be in the controller
You are correct. It shouldn't be in the view neither due to the absolute mess it would create. I think this code is better suited in a custom HTML helper:
public static class HtmlExtensions
{
public static IHtmlString FormatDetails(this HtmlHelper htmlHelper, IEnumerable<string> details)
{
var init = "yes";
var html = new StringBuilder();
foreach (var item in details)
{
if (item.Substring(0, 1) != " ")
{
if (init != "yes")
{
html.Append("</ul>");
}
html.AppendFormat("<p>{0}</p><ul>", htmlHelper.Encode(item));
}
else
{
html.AppendFormat("<li>{0}</li>", htmlHelper.Encode(item.Substring(1)));
}
}
return MvcHtmlString.Create(html.ToString());
}
}
which you would invoke in your view:
#Html.FormatDetails(Model.Details)
Remark: there seems to be something wrong with the init variable. You are setting its value to yes initially but you never modify it later.