razor

Access Viewbag property on all views

…衆ロ難τιáo~ 提交于 2019-12-21 04:01:11
问题 How can I access some ViewBag properties across all my views? I want to have some information like current user name, etc accessible everywhere, but without having to to specifically define the properties in each ActionResult method on my project 回答1: The best and straight forward way to accomplish your requirement is to make a Custom Base Controller and inherit your Controller from this Base Controller. public class MyBaseController : Controller { protected override void OnActionExecuting

How to apply input which has a type email to to HTML Helper in Asp.net MVC3 Razor

北城以北 提交于 2019-12-21 03:51:15
问题 How to apply input which has a type email to HTML Helper in Asp.net MVC3 Razor. For example: <input type="email" name="name" value=" " placeholder="example@mail.ru" /> Is there alternative in Razor? 回答1: You can use Html.TextBox method. @Html.TextBox("name","", new { type = "email", placeholder = "example@mail.ru" }) 回答2: Use Dataanotations at Model : [Required] [DataType(DataType.EmailAddress, ErrorMessage = "Invalid Email Address")] public string EmailAddress { get; set; } 回答3: The below

How can I set a variable on a _Layout page?

喜你入骨 提交于 2019-12-21 03:42:36
问题 I have a view and I want to use a layout page. In the layout page I want to have a conditional banner which some of the view will turn on/off. Just wondering how I can do this? I have this in the _Layout.cshtml page... @if (ShowBanner){ <h1>banner</h1> } I'm wondering how I can turn this on/off from my MVC View page? Or whether this is the right thing to do at all? I mean if I declare that variable in the View page surely the master doesn't know about it? How do the two communicate through c#

ASP.Net MVC 3 Razor Response.Write position

放肆的年华 提交于 2019-12-21 03:35:10
问题 I am trying to update this tutorial on implementing Facebooks BigPipe to razor. There is a html helper extension that adds a pagelet to a list, and then outputs a holding div to the response. The idea is that later on the content of this pagelet is rendered to a string, and then injected into this holding div via javascript. public static void RegisterPagelet(this HtmlHelper helper, Pagelet pagelet) { var context = helper.ViewContext.HttpContext; List<Pagelet> pagelets = (List<Pagelet>

Extend MVC3 razor Html.LabelFor to add css class

百般思念 提交于 2019-12-21 03:35:07
问题 I am trying to add a css class to Html.LabelFor on an EditorTemplate @Html.LabelFor(model => model.Name, new { @class = "myLabel" }) my expectation for instance:label should pick up the css class. For this I tried to extend Label with the following code ,but my label is not showing up . What am I doing wrong here ? public static class LabelExtensions { public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object

RedirectToLocal not found

梦想与她 提交于 2019-12-21 03:17:09
问题 I have this code : using Solutionsecurity.web.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Security; namespace Solutionsecurity.web.Controllers { public class HomeController : Controller { public ActionResult Login() { return View(new User()); } [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Login(User u, string ReturnUrl) { if (Membership.ValidateUser(u.login, u.password)) {

Sharing views between ASP.NET MVC 3 Web application projects

纵然是瞬间 提交于 2019-12-21 02:30:44
问题 I have several ASP.NET MVC 3 Web applications that have a lot of common objects: The same login controller. The few differences between their login systems are captured in their web.config files. Header/detail views: invoices, payment orders, payment orders, etc. In order to make my code less redundant, I moved common controllers and views to a separate class library (models were already in their own class libraries). After googling for a while, I eventually found out how to call controllers

Set a property on ViewBag dynamic object in F#

眉间皱痕 提交于 2019-12-21 02:28:16
问题 I have this action method in C#: public ActionResult Index() { ViewBag.Message = "Hello"; return View(); } And this view (Index.cshtml): <h2>@ViewBag.Message</h2> And this produces the expected "Hello" on the page. I want to do the controller in F#. I've tried type MainController() = inherit Controller() member x.Index() = x.ViewBag?Message <- "Hello" x.View() And this produces the error message "Method or object constructor 'op_DynamicAssignment' not found". I've looked at some of the F#

asp.net mvc - pass partial data model to partial view

谁说我不能喝 提交于 2019-12-20 23:11:52
问题 I wish to build a partial view that gets a model column and print it. Something like that: At the view: @model IEnumerable<products_comparison.Models.Product> @{ ViewBag.Title = "Index"; var Brand = (from r in Model select r.Brand).Distinct(); } <h2> Index</h2> @Html.RenderPartial("_DisplayAttribute",Brand) And at the partial view: <table> <tr> <th> Brand </th> </tr> @foreach (var row in Model) { <tr> <td> @Html.DisplayFor(r => row) </td> </tr> } </table> There are a few problems I run into:

Recommended approach to implementing inline editing for a MVC grid please?

两盒软妹~` 提交于 2019-12-20 20:35:22
问题 I am using MVC3, C#, Razor, EF4.1 I have implemented grids in their most simple form ie Razor Tables. At present I have implemented editing of record fields off page ie Click "Edit" and the edit page appears, one then fills in data then save which returns user to main grid page. I need an inline solution where only 1 or 2 fields need updating. Typically the user would either click on the row or on "edit" link and the row would change to "edit mode". One would then edit the data. One would