asp.net-mvc-2

How can I use Html.DisplayFor inside of an iterator?

白昼怎懂夜的黑 提交于 2019-11-27 13:53:00
I am loving MVC 2. The whole thing just fits the web so well. There is one piece of functionality, however, that I am unable to coax out of the Html.DisplayFor() function: <@ Page Inherits="ViewPage<IEnumerable<Foo>>"> <% foreach(var item in Model) { %> <%: Html.DisplayFor(item.BarBaz) %> <% } %> I need to be able to use the DisplayTemplate for this value. Is there a way to do this? Actually, I figured it out. How stupid of me. This works: <@ Page Inherits="ViewPage<IEnumerable<Foo>>"> <% foreach(var item in Model) { %> <%: Html.DisplayFor(m => item.BarBaz) %> <% } %> You can accomplish it by

Current date and time - Default in MVC razor

淺唱寂寞╮ 提交于 2019-11-27 13:51:41
问题 When the MVC view page with this textbox, loads , I would like to display current date and time by default. How can I do this? in razor. @Html.EditorFor(model => model.ReturnDate) 回答1: Before you return your model from the controller, set your ReturnDate property to DateTime.Now() myModel.ReturnDate = DateTime.Now() return View(myModel) Your view is not the right place to set values on properties so the controller is the better place for this. You could even have it so that the getter on

MVC 3 Editor Template with DateTime

混江龙づ霸主 提交于 2019-11-27 13:38:22
问题 I want to change this code from MVC2 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime?>" %> <%=Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() : string.Empty), new { @class = "datePicker" }) %> To MVC 3 using Razor. You can find the full post of what I want to do here 回答1: @model DateTime? @Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() : string.Empty), new { @class = "datePicker" }) 来源: https://stackoverflow.com

.NET MVC : Calling RedirectToAction passing a model?

岁酱吖の 提交于 2019-11-27 13:28:47
I got a view List.aspx that is bound to the class Kindergarten In the controller: public ActionResult List(int Id) { Kindergarten k = (from k1 in _kindergartensRepository.Kindergartens where k1.Id == Id select k1).First(); return View(k); } That works. But this doesn't [AcceptVerbs(HttpVerbs.Post)] public ActionResult Add(...) { //... Kindergarten k = ... return RedirectToAction("List", k); } How should I redirect to the list view, passing k as the model? Thanks! I think you just need to call view like return RedirectToAction("List", new {id}); with id you need to populate the Kindergarten. I

Capturing HTML output with a controller action filter

岁酱吖の 提交于 2019-11-27 13:17:51
问题 I've got the following filter in place on an action to capture the HTML output, convert it to a string, do some operations to modify the string, and return a ContentResult with the new string. Unfortunately, I keep ending up with an empty string. private class UpdateFilter : ActionFilterAttribute { private Stream stream; public override void OnActionExecuting(ActionExecutingContext filterContext) { stream = filterContext.HttpContext.Response.Filter; stream = new MemoryStream(); filterContext

Plus (+) in MVC Argument causes 404 on IIS 7.0

旧街凉风 提交于 2019-11-27 12:47:53
问题 I have an MVC route that is giving me hell on a staging server running IIS. I am running Visual Studio 2010's development server locally. Here is a sample URL that actually works on my dev box: Root/CPUBoards/Full+Size Results Server Error404 - File or directory not found. The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable. Here is the complete behaviour I am seeing. Localhost: Root/CPUBoards/Full Size - Resolves Root/CPUBoards/Full

The type arguments cannot be inferred from the usage. Try specifying the type arguments explicitly

China☆狼群 提交于 2019-11-27 12:44:36
问题 Could someone please clarify something for me. In my ASP.NET MVC 2 app, I've got a BaseViewModel class which includes the following method: public virtual IDictionary<string, object> GetHtmlAttributes<TModel, TProperty> (Expression<Func<TModel, TProperty>> propertyExpression) { return new Dictionary<string, object>(); } The idea being that each child viewmodel can override this method and provide a suitable set of html attributes, based on some logic, to be rendered in the view: <%: Html

.NET MVC Custom Date Validator

心不动则不痛 提交于 2019-11-27 12:42:28
问题 I'll be tackling writing a custom date validation class tomorrow for a meeting app i'm working on at work that will validate if a given start or end date is A) less than the current date, or B) the start date is greater than the end date of the meeting (or vice versa). I think this is probably a fairly common requirement. Can anyone point me in the direction of a blog post that might help me out in tackling this problem? I'm using .net 3.5 so i can't use the new model validator api built into

MVC - Redirect inside the Constructor

别说谁变了你拦得住时间么 提交于 2019-11-27 12:30:23
问题 I would like to know how am I able to redirect the request inside the controller constructor if I need to do it?. For example: Inside the constructor I need to initialize an object with an dynamic value, in some cases I don't want to do it and in that case I want to redirect to some other place. At the same way the rest of the constructor will not be executed neither the "original following action". How can I do it? Thank you EDIT #1 Initially I used: public override void OnActionExecuting

How to concatenate several MvcHtmlString instances

会有一股神秘感。 提交于 2019-11-27 11:37:52
问题 I have some doubts about how to concatenate MvcHtmlString instances because of this information found in MSDN : MvcHtmlString Class Represents an HTML-encoded string that should not be encoded again Do I risk that strings are HTML-encoded twice when using code like this: var label = Html.LabelFor(model => model.Email); var textbox = Html.TextBoxFor(model => model.Email); var validation = Html.ValidationMessageFor(model => model.Email); var result = MvcHtmlString.Create( label.ToString() +