asp.net-mvc-2

“The Id field is required” validation message on Create; Id not set to [Required]

╄→гoц情女王★ 提交于 2019-11-28 03:53:26
This is happening when I try to create the entity using a Create style action in Asp.Net MVC 2. The POCO has the following properties: public int Id {get;set;} [Required] public string Message {get; set} On the creation of the entity, the Id is set automatically, so there is no need for it on the Create action. The ModelState says that "The Id field is required", but I haven't set that to be so. Is there something automatic going on here? EDIT - Reason Revealed The reason for the issue is answered by Brad Wilson via Paul Speranza in one of the comments below where he says (cheers Paul): You're

ASP.NET MVC 3 Custom HTML Helpers- Best Practices/Uses

徘徊边缘 提交于 2019-11-28 02:45:41
New to MVC and have been running through the tutorials on the asp.net website. They include an example of a custom html helper to truncate long text displayed in a table. Just wondering what other solutions people have come up with using HTML helpers and if there are any best practices or things to avoid when creating/using them. As an example, I was considering writing a custom helper to format dates that I need to display in various places, but am now concerned that there may be a more elegant solution(I.E. DataAnnotations in my models) Any thoughts? EDIT: Another potential use I just

How to implement badges?

左心房为你撑大大i 提交于 2019-11-28 02:40:38
I've given some thought to implementing badges (just like the badges here on Stack Overflow) and think it would be difficult without Windows services, but I'd like to avoid that if possible. I came up with a plan to implement some examples: Audobiographer: Check if all fields in the profile is filled out. Commentor: When making a comment check if the number of comments equal 10, if so award the badge. Good Answer: When voting up check to see if vote score is 25 or higher. How could this be implemented in the database? Or would another way be better? A similar-to-Stackoverflow implementation is

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

折月煮酒 提交于 2019-11-28 02:11:32
问题 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. 回答1: 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

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

纵然是瞬间 提交于 2019-11-28 02:10:48
问题 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,

Can my MVC2 app specify route constraints on Query String parameters?

我的未来我决定 提交于 2019-11-28 02:03:11
My MVC2 app uses a component that makes subsequent AJAX calls back to the same action, which causes all kinds of unnecessary data access and processing on the server. The component vendor suggests I re-route those subsequent requests to a different action. The subsequent requests differ in that they have a particular query string, and I want to know whether I can put constraints on the query string in my route table. For example, the initial request comes in with a URL like http://localhost/document/display/1 . This can be handled by the default route. I want to write a custom route to handle

HttpHandler Property Injection using Ninject returning null

允我心安 提交于 2019-11-28 02:01:41
问题 I have the following httphandler: public class NewHandler : IHttpHandler { [Inject] public IFile FileReader { get; set; } public NewHandler() { } public void ProcessRequest(System.Web.HttpContext context) { .... var something = SomeMethod(FileReader); .... } public bool IsReusable { get { return true; } } } This is my Ninject Module in the Global.asax. internal class ServiceModule : NinjectModule { public override void Load() { Bind<IFile>().To<FileWrapper>().InSingletonScope(); } } Every

HttpContext.Current.Server null

喜夏-厌秋 提交于 2019-11-28 01:59:51
I have a windows service which is using a method from a class library with same asp.net solution. in class library, I have a method with following line: reader = XmlReader.Create(HttpContext.Current.Server .MapPath("~/TestDevice/Data.xml"), settings); When control comes to this line. I get exception. I tried to debug the code and found that when service tries to access this method then HttpContext.Current.Server is null . What is alternative syntax. I tried to access this class library method from web application and it works fine. Please suggest solution. HttpContext.Current is returning null

Change the Views location

与世无争的帅哥 提交于 2019-11-28 01:54:46
问题 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

Convert JSON dates without timezone

泄露秘密 提交于 2019-11-28 01:17:58
问题 I have a webservice that returns for example a DateTime object: DepartureDate. I use ajax to fetch this and in my view I convert the JSON date string to a javascript date object with this function: function convertToDate(jsonDate) { return eval("new " + jsonDate.substring(1, jsonDate.length - 1)); } The problem is that new Date() takes the local time on the clients computer in consideration, so clients in different countries get different dates. I want to get the exact date that was returned