razor

Are lambda expressions supported by Razor?

萝らか妹 提交于 2019-12-31 19:24:08
问题 Are lambda expressions/anonymous methods supported in the Razor view engine? I am having difficulty expressing the following in Razor: @Model.ToList().ForEach(i => { if (i.DealerName != null) { <text> @i.DealerName </text> } } Note: I know can solve this with @foreach but I need a similar solution for a 3rd party MVC control. It using this mechanism for setting the content of the control. It works fine for MVC .ASPX views but cannot get it to work with Razor. MVC .ASPX equivalent (the code I

How to implement multiple select in MVC 3 using 'chosen.js' plugin

╄→гoц情女王★ 提交于 2019-12-31 17:10:34
问题 How to implement chosen plugin for MVC 3 ? for this type of output 回答1: This is my code how to make chosen.js work with javascript/MVC This is my code for my dropdown @Html.DropDownListFor(m => m.CategoryId, new SelectList(Model.Categories, "Id", "Name"), "Choose a Category...", new { id = "CategoryId", multiple = "", @class = "chzn-select srs-select search-dropdown", data_placeholder = "Choose a Category..." }) Here I use 'chzn-select' styling -- In the document ready, one should have the

Assigning the Model to a Javascript variable in Razor

喜欢而已 提交于 2019-12-31 14:35:20
问题 I have a strongly-typed view bound to an object which contains a Collection (list) of some objects. I know Razor gets executed on the server-side when it's generating the page, whereas Javascript variables don't get instantiated until after the page is displayed... but would it somehow be possible to convert the Model (that the view is bound to) or any of its fields to JSON in Razor without resorting to an AJAX call to fetch that data afterwards? You know, something like... var

How to set contenttype in razor (CSHTML)?

∥☆過路亽.° 提交于 2019-12-31 13:13:44
问题 In classic ASP.NET Web Forms view engine, we can set the ContentType in the .aspx page to the desired type. Is there a direct/recommended equivalent in Razor? 回答1: You should set a different content type in your action method. public ActionResult MyAction() { Response.ContentType = "something"; return View(); } 回答2: That will work I just tested it, you can also add the following line to your cshtml: Response.ContentType = "application/javascript"; so that it looks something like this: @{

How to set contenttype in razor (CSHTML)?

天大地大妈咪最大 提交于 2019-12-31 13:13:17
问题 In classic ASP.NET Web Forms view engine, we can set the ContentType in the .aspx page to the desired type. Is there a direct/recommended equivalent in Razor? 回答1: You should set a different content type in your action method. public ActionResult MyAction() { Response.ContentType = "something"; return View(); } 回答2: That will work I just tested it, you can also add the following line to your cshtml: Response.ContentType = "application/javascript"; so that it looks something like this: @{

Display a table/list data dynamically in MVC3/Razor from a JsonResult?

旧街凉风 提交于 2019-12-31 12:58:31
问题 I have a view page, users.cshtml. And I have a JsonResult action method, jsongetusers() that returns the list of users in json format. On users.cshtml page load, I want to get the list of users, build a table and display it. What is the best way to implement this in ASP.Net MVC using Razor? I am pretty new to MVC3 and Razor. My initial thought was to loop through the json result and build a table using javascript/jquery and append it to the DOM. But I am guessing there must be a better way to

How to tell razor NOT to html escape

空扰寡人 提交于 2019-12-31 10:57:33
问题 I am using asp.net mvc 3 with razor for a project. At some I need to serialize an array from the controller, put it in the viewdata and assign it to a js object. However when I output it using @ViewData["some array"] The result is html escaped so i get something like : [{"title":"Something","id":"Something-1" etc' With the <%= %> this was not escaped so it was behaving as expected. Is it possible to tell razor not to escape this string. Perhaps, someone might suggest another approach all

Cannot use NuGet PagedList ASP.NET MVC # View

倖福魔咒の 提交于 2019-12-31 10:39:12
问题 I can't use the following namespace PagedList to use razor @model PagedList.IPagedList<PhoneBook.Models.Contact> in the topmost of view Index.cshtml I already installed PagedList and i used already the code below in my Controller using PagedList; there's no error in the controller page but why ican't use the namespace in Index.cshtml (View)? Please help.. 回答1: BuildStarted referral or existing on at the same time use, in controller using PagedList; using PagedList.Mvc; and use in view @model

How can I get the property name in my MVC3 custom Editor Template

吃可爱长大的小学妹 提交于 2019-12-31 08:54:09
问题 I have an enumeration in my project and I've created a custom editor template for this enumeration. So, now any model I have with a property with a type of this enumeration, will render a drop down. This works great, but I would like to name my select element of my dropdown with the name of the property. Here is my Razor code for my editor template. @model ItemEnumerations.ItemType <select id="PropertyNameHere" name="PropertyNameHere"> @foreach (ItemEnumerations.ItemType in Enum.GetValues

Turning off the WebFormViewEngine when using razor?

不问归期 提交于 2019-12-31 08:41:47
问题 I downloaded Glimpse this morning to try it out and noticed this when I click on the views tab: It checks all of the loaded view engines. I found where the RazorViewEngine is specified in web.config, but I couldn't find where the WebFormViewEngine was. Since I know my project will never have an web form view in it, Is it ok/safe to turn off WebFormViewEngine ? How can I turn off WebFormViewEngine ? 回答1: It is perfectly OK to remove the web forms view engine if you are not using it. You can do