razor

How can I make Html.CheckBoxFor() work on a string field?

ぐ巨炮叔叔 提交于 2019-12-18 14:56:05
问题 I'm using ASP.NET MVC3 with Razor and C#. I am making a form builder of sorts, so I have a model that has a collection of the following object: public class MyFormField { public string Name { get; set; } public string Value { get; set; } public MyFormType Type { get; set; } } MyFormType is just an enum that tells me if the form field is a checkbox, or textbox, or file upload, or whatever. My editor template looks something like this (see the comment): ~/Views/EditorTemplates/MyFormField

@Html.DropDownListFor How to add option?

♀尐吖头ヾ 提交于 2019-12-18 14:06:10
问题 @Html.DropDownListFor(model => model.ZipFile, new SelectList(ViewBag.ZipFiles)) The above code creates me a select list just fine. But I want to make the selection optional. Unfortunately there is no empty option and I'd like to add one in. How would I do this? 回答1: By using the proper DropDownListFor overload: @Html.DropDownListFor( model => model.ZipFile, new SelectList(ViewBag.ZipFiles), "-- please select a zip file --" ) 回答2: @Html.DropDownListFor(model => model.Country, new List

ASP.NET MVC4 dynamic form generation

廉价感情. 提交于 2019-12-18 13:54:34
问题 I am new to ASP.NET MVC and need some help. I have a need to create a form where the form is dynamically created. For example let's say I have a model named Person. The Person has a first and last name. In the form I want to allow the user to enter the first and last name of the person. I know how to do that, but what I don't know how to do is allow the user to add multiple persons and make that a single form submission. On the initial load of the page (view) it would by default show two

Change id attribute of an html.editorfor helper in MVC4 with Razor

狂风中的少年 提交于 2019-12-18 13:31:36
问题 I have looked through some various answers related to this but all were for mvc3 or not for Razor. I have a single page that has multiple forms, in partial views, that are for editing a different models. However, most of those models have a 'name' field. I am looking to be able to specify an editorfor with a specific id as such: <div class="editor-label"> @Html.LabelFor(model => model.Name) </div> <div class="editor-field"> @Html.EditorFor(model => model.Name, new {id = "PersonName"}) @Html

How to put span element inside ActionLink MVC3?

左心房为你撑大大i 提交于 2019-12-18 13:29:35
问题 How can I put a span element inside ActionLink BUT NOT WITH URL.ACTION? This: <li><span> @Ajax.ActionLink("LinkText", "ControllerName", new AjaxOptions { UpdateTargetId = "div", InsertionMode = InsertionMode.Replace, HttpMethod = "GET", LoadingElementId = "progress" }) </span></li> generates this: <li> <span> <a href="/Home/ControllerName" data-ajax-update="#scroll" data-ajax-mode="replace" data-ajax-method="GET" data-ajax-loading="#progress" data-ajax="true">LinkText</a> </span> </li> But I

How to set a Razor layout in MVC via an attribute filter?

孤者浪人 提交于 2019-12-18 13:25:12
问题 I'd like to set a default Razor layout via code in either a base controller or an attribute. It was mentioned in the Docs that this is possible, but I can't figure out how it's done. I know there's the masterPage parameter to the View method available, but I would like all views returned by a controller to have this value set automatically. And no, I can't use _ViewStart for this since my views are going to be in various places (this is not a normal MVC site configuration). Thanks 回答1: I

ASP.NET MVC 3 Model-binding and form fields

自古美人都是妖i 提交于 2019-12-18 13:20:36
问题 I have an entity called Domain.Models.BlogPost which contains the following properties: PostID Title Author PostedDate Body I also have an entity called Domain.Models.PostComment which contains the following properties: CommentID PostID Author Email Website Body BlogPost contains many PostComments . A one to many relationship. Now I have a view like this (separated comment form from blog post code via html comment): @model Domain.Models.BlogPost @using Domain.Models; @{ ViewBag.Title = "Post"

Razor page lifecycle in ASP.NET MVC

半城伤御伤魂 提交于 2019-12-18 13:20:03
问题 My question in general about render pipeline, i have seen ASP.NET MVC pipeline scheme and there was a step called View Engine, so how it works? I want to know about this scenarios: What is rendered first, master page or view? If i use Response.End() in @{} block at the start of page does this interupt execution of page and stops render of the view? 回答1: What is rendered first, master page or view? The view. The parser starts from the Layout and builds a LIFO (Last In First Out) structure

Using html helpers in Razor web helper

前提是你 提交于 2019-12-18 13:08:15
问题 I am trying to create a Razor web helper something like this : @helper DisplayForm() { @Html.EditorForModel(); } But this gives the error "CS0103: The name 'Html' does not exist in the current context" . Is there any way to reference html helpers within web helpers? 回答1: You can cast the static Page property from the context to the correct type: @helper MyHelper() { var Html = ((System.Web.Mvc.WebViewPage)WebPageContext.Current.Page).Html; Html.RenderPartial("WhatEver"); @Html.EditorForModel(

Does the output of JsonConvert.SerializeObject need to be encoded in Razor view?

时间秒杀一切 提交于 2019-12-18 13:00:48
问题 I use the Newtonsoft library to convert C# objects into JSON. Is this use of Newtonsoft.Json.JsonConvert.SerializeObject secure, or is additional encoding necessary? If additional encoding is needed, what do you suggest? Here is how I use it in a Razor view: <script type="text/javascript"> var jsModel = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model)) </script> 回答1: You will at the very least need to perform additional encoding of the '<' character to '\u003C' and the '>'