asp.net-mvc-2

ASP.NET MVC ModelMetaData: Is there a way to set IsRequired based on the RequiredAttribute?

偶尔善良 提交于 2019-11-29 12:06:00
问题 Brad Wilson posted a great blog series on ASP.NET MVC's new ModelMetaData: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-2-modelmetadata.html In it, he describes how the ModelMetaData class is now exposed in the Views and templated helpers. What I'd like to do is display an asterisk beside a form field label if the field is required, so I thought about using the IsRequired property of ModelMetaData. However, IsRequired by default is true for all non-nullable

Link to a root controller from area controller in ASP MVC

混江龙づ霸主 提交于 2019-11-29 11:58:11
问题 How can I link to one of my root controllers from one of my areas? <% Html.RenderAction("Action", "Page", new {area = "root", name = "Admin"}); %> This gives me an error: No route in the route table matches the supplied values. I have a controller named Page in a folder named Admin in my root controller collection. I can reach this controller by typing \Admin\Page. This is how I registered the route: routes.MapRoute( "Admin", "Admin/{controller}/{action}/{id}", new { controller = "Admin",

Using MVC's AuthorizeAttribute with multiple groups of Roles?

我与影子孤独终老i 提交于 2019-11-29 10:59:13
问题 What I want to do is a two-level role check on an action handler. For example, Require that the users is in at least one of the following groups: SysAdmins, Managers AND in at least one of the following groups: HR, Payroll, Executive. Initial guess was that this might be the way to do this but I don't think it is: [Authorize(Role="SysAdmins,Managers")] [Authorize(Role="HR,Payroll,Executive")] public ActionResult SomeAction() { [...] } Do I need to role my own custom Attribute to take in Role1

Validation: Model or ViewModel

你。 提交于 2019-11-29 10:58:55
问题 Where should validation reside when using ViewModels with MVC (MVVM), on the Model or the ViewModel? (Or both or neither)? And why? I bring this up especially in light of V2 of ASP.NET MVC coming out soon. What about complex/custom validation? 回答1: Any business specific validation should be in the Model. Any web site specific validation could be done in both the Model and/or ViewModel. This way your business logic (Models) can move behind a different interface (Windows, Web Forms, etc.) and

Radio Button generates duplicate HTML id-s

故事扮演 提交于 2019-11-29 10:40:23
问题 It seems that the default ASP.NET MVC2 Html helper generates duplicate HTML IDs when using code like this (EditorTemplates/UserType.ascx): <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<UserType>" %> <%: Html.RadioButton("", UserType.Primary, Model == UserType.Primary) %> <%: Html.RadioButton("", UserType.Standard, Model == UserType.Standard) %> <%: Html.RadioButton("", UserType.ReadOnly, Model == UserType.ReadOnly) %> The HTML it produces is: <input checked="checked" id=

DataAnnotation Validations and Custom ModelBinder

允我心安 提交于 2019-11-29 10:35:37
问题 I've been running some experiments with ASP.NET MVC2 and have run into an interesting problem. I'd like to define an interface around the objects that will be used as Models in the MVC app. Additionally, I'd like to take advantage of the new DataAnnotation functionally by marking up the members of this interface with validation attributes. So, if my site has a "Photo" object, I'll define the following interface: public interface IPhoto { [Required] string Name { get; set; } [Required] string

MVC MapPageRoute and ActionLink

主宰稳场 提交于 2019-11-29 09:57:47
I have created a page route so I can integrate my MVC application with a few WebForms pages that exist in my project: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); // register the report routes routes.MapPageRoute("ReportTest", "reports/test", "~/WebForms/Test.aspx" ); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } This has created a problem whenever I use Html.ActionLink in my Views: <%: Html.ActionLink(

Is there a way to validate incoming HttpPostedFilebase files in MVC 2?

核能气质少年 提交于 2019-11-29 08:53:24
I have a couple of files I need to save in addition to some simple scalar data. Is there a way for me to validate that the files have been sent along with the rest of the form data? I'm trying to use the [Required] attribute, but it doesn't seem to be working. The following worked for me. Model: public class MyViewModel { [Required] public HttpPostedFileBase File { get; set; } } Controller: public class HomeController : Controller { public ActionResult Index() { return View(new MyViewModel()); } [HttpPost] public ActionResult Index(MyViewModel model) { if (!ModelState.IsValid) { return View

Is there a better way to update my entities with the Entity framework?

杀马特。学长 韩版系。学妹 提交于 2019-11-29 08:48:22
I have not really a "problem" but I found the way I develop this code not really well. I have my countries controller (Edit method) (WebUI layer): [HttpGet] public ActionResult Edit(int id) { var country = _groupsRepository.getCountryById(id); Mapper.CreateMap<Country, CountriesEditViewModel>(); CountriesEditViewModel viewModel = Mapper.Map<Country, CountriesEditViewModel>(country); return View(viewModel); } // // POST: /CountriesAdmin/Edit/5 [HttpPost] public ActionResult Edit(int id, CountriesEditViewModel viewModel) { try { if (ModelState.IsValid) { Mapper.CreateMap<CountriesEditViewModel,

Change the Views location

女生的网名这么多〃 提交于 2019-11-29 08:11:41
I am developing a website in MVC 2.0. I want to change the View folder location in my website. I wanted to keep the views folder inside other folders, When I try to do so i am getting following errors The view 'Index' or its master was not found. The following locations were searched: ~/Views/Search/Index.aspx ~/Views/Search/Index.ascx ~/Views/Shared/Index.aspx ~/Views/Shared/Index.ascx Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. My Views