razor

Sharing Razor views across projects

≡放荡痞女 提交于 2019-12-29 05:44:06
问题 I want to share the layout (Header, Navigation and Footer Razor views) across multiple ASP.NET MVC projects. How can I do that? Can I create a custom NuGet package to wrap the common Razor files, images and CSS? 回答1: Three approaches: Share the Razor view source code using your version control system Compile the views into a separate DLL file for binary sharing. Create a NuGet package See Compile your ASP.NET MVC Razor views into a separate DLL for how you do option 2. For option 3, see

Using Static Variables in Razor

寵の児 提交于 2019-12-29 05:44:06
问题 Why is it not possible to use a static Variable from a static class inside a view? For example, lets say you have a Settings Class: public static class GlobalVariables { public static string SystemColor { get { return Properties.Settings.Default.SystemColor; } } } Why wouldn't you be able to call it in a view? like so @using AppName.Models <html> <div ><h1 style="color:@GlobalVariables.SystemColor">System Color</h1></div> </html> 回答1: As far as I'm aware, you can access static variables from

Create using for own helper? like Html.BeginForm

此生再无相见时 提交于 2019-12-29 04:45:13
问题 I was wondering, is it possible to create your own helper definition, with a using? such as the following which creates a form: using (Html.BeginForm(params)) { } I'd like to make my own helper like that. So a simple example I'd like to do using(Tablehelper.Begintable(id) { <th>content etc<th> } which will output in my view <table> <th>content etc<th> </table> Is this possible? if so, how? Thanks 回答1: Sure, it's possible: public static class HtmlExtensions { private class Table : IDisposable

ASP.NET MVC 3 Razor : Initialize a JavaScript array

允我心安 提交于 2019-12-29 04:34:12
问题 What would be the prefered way to initialize a JS array in ASP.NET MVC 3 with Razor with a value I have in my model/view model ? For example to initialize an array of strings representing dates : <script type="text/javascript"> var activeDates = ["7-21-2011", "7-22-2011"]; </script> with public class MyViewModel { public DateTime[] ActiveDates { get; set; } } 回答1: I don't quite understand the relation between JS and ASP.NET MVC 3 Razor. Javascript runs on the client side no matter which

“ASP.global_asax does not exist in the namespace ASP”

こ雲淡風輕ζ 提交于 2019-12-29 02:45:08
问题 I created a RazorFunctions.cshtml file on App_Code @functions { public static string GetActiveClassIf(string controllerName, string actionName = null) { var routeData = @HttpContext.Current.Request.RequestContext.RouteData; string currentController = routeData.Values["controller"].ToString(); string currentAction = routeData.Values["action"].ToString(); return controllerName == currentController && (String.IsNullOrEmpty(actionName) || currentAction == actionName) ? "active" : ""; } } and when

how to include AntiForgeryToken in an ajax action link in mvc?

不想你离开。 提交于 2019-12-29 01:25:18
问题 I have the following code: @Ajax.ActionLink("Delete", "Delete", new { id = item.ID, RequestVerificationToken=*What comes here?*}, new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "formsIndex" }) I want to add the verification token to the link without using javascript in client side, it seems like a redundant dependancy since i already own that value in server. Is there a proper way to do that? 回答1: From the MSDN documentation (my emphasis) HtmlHelper.AntiForgeryToken Method Generates

how to include AntiForgeryToken in an ajax action link in mvc?

血红的双手。 提交于 2019-12-29 01:25:08
问题 I have the following code: @Ajax.ActionLink("Delete", "Delete", new { id = item.ID, RequestVerificationToken=*What comes here?*}, new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "formsIndex" }) I want to add the verification token to the link without using javascript in client side, it seems like a redundant dependancy since i already own that value in server. Is there a proper way to do that? 回答1: From the MSDN documentation (my emphasis) HtmlHelper.AntiForgeryToken Method Generates

MVC 3 ASPX VS RAZOR View Engine [closed]

跟風遠走 提交于 2019-12-28 20:35:13
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . Hi i just downloaded MVC 3 and found an new view engine called "RAZOR". How is it useful? What benefits does it provide? How is different from ASPX pages? Where should one use RAZOR view engine? 回答1: Scott Gu covered most of your questions in his Razor introductory blog post:

bind drop down list using jquery ajax on change of first ddl

时光毁灭记忆、已成空白 提交于 2019-12-28 18:20:05
问题 I have two drop down lists, onchange of first drop downlist i want to populate the second one in ajax. I get the SelectListItem in ajax how to pass that to drop down list to bind it? view: @Html.DropDownList("FirstID", ViewBag.Groups as IEnumerable<SelectListItem> ) @Html.DropDownList("SecondID", ViewBag.Policies as IEnumerable<SelectListItem>) Ajax method in view: $(function () { $('#FirstID').change(function () { var selectedValue = $(this).val(); $.ajax({ url: '@Url.Action(

ASP.NET MVC Razor, Html.BeginForm, using statement

不想你离开。 提交于 2019-12-28 16:58:50
问题 For an ASP.NET MVC application, can someone explain to me why calls to Html.BeginForm begin with the statement @using ? Example - @using (Html.BeginForm()) { //Stuff in the form } I thought @using statements are for including namespaces. Thanks! 回答1: Using Statement provides a convenient syntax that ensures the correct use of IDisposable objects. Since the BeginForm helper implements the IDisposable interface you can use the using keyword with it. In that case, the method renders the closing