asp.net-mvc-2

Why does ASP.NET MVC care about my read only properties during databinding?

爱⌒轻易说出口 提交于 2019-12-17 16:05:08
问题 Edit: Added bounty because I'm seeking an MVC3 solution (if one exists) other than this: DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false; I have a read only property on my 'Address' model 'CityStateZip' . It's just a convenient way to get city, state, zip from a US address. It throws an exception if the country is not USA (the caller is supposed to check first). public string CityStateZip { get { if (IsUSA == false) { throw new ApplicationException(

TextBoxFor rendering to HTML with prefix on the ID attribute

a 夏天 提交于 2019-12-17 15:58:32
问题 I have an ASPNET MVC 2 project. When I use <%= Html.TextBoxFor(model => model.Login) %> the TexBoxFor will render as <input id="Login" name="Login" type="text" value="" /> Field in the model is [Required(ErrorMessage = "")] [DisplayName("Login")] public string Login { get; set; } Can I made id and name attribute with some prefix? Like <input id="prefixLogin" name="prefixLogin" type="text" value="" /> Thanks to all. 回答1: It seems MVC 2 RTM does not currently provide this feature. You can try

How to set a default value with Html.TextBoxFor?

≯℡__Kan透↙ 提交于 2019-12-17 15:27:06
问题 Simple question, if you use the Html Helper from ASP.NET MVC Framework 1 it is easy to set a default value on a textbox because there is an overload Html.TextBox(string name, object value) . When I tried using the Html.TextBoxFor method, my first guess was to try the following which did not work: <%: Html.TextBoxFor(x => x.Age, new { value = "0"}) %> Should I just stick with Html.TextBox(string, object) for now? 回答1: you can try this <%= Html.TextBoxFor(x => x.Age, new { @Value = "0"}) %> 回答2

How to URL encode parameters in ASP .NET MVC

不打扰是莪最后的温柔 提交于 2019-12-17 14:47:41
问题 I have the following code in my view: <%= Html.ActionLink( "View item", "Index", "Items", new { itemName = Model.ItemName }, null) %> I have a problem when the item name contains a sharp (#) or the percent symbol (%). When the item name is "name#with#sharp#" , the controller receives only the first part of the name until the first sharp (only receives "name" ). When the item name is "name%with%percent" I get an error: HTTP error 400 - Bad request. I not sure if this is a problem with the URL

.NET MVC : Calling RedirectToAction passing a model?

瘦欲@ 提交于 2019-12-17 10:52:33
问题 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! 回答1: I think you just need to

JSONResult to String

不打扰是莪最后的温柔 提交于 2019-12-17 10:25:35
问题 I have a JsonResult that is working fine, and returning JSON from some POCO's. I want to save the JSON as a string in a DB. public JsonResult GetJSON() { JsonResult json = new JsonResult { Data = GetSomPocos() }; return json; } I need to audit the response, so I want to save the json into a DB. I am having trouble finding a way to get the JSON as a string. Any help is appreciated. 回答1: You're looking for the JavaScriptSerializer class, which is used internally by JsonResult: string json = new

Asp.net mvc override OnException in base controller keeps propagating to Application_Error

可紊 提交于 2019-12-17 10:18:24
问题 I am trying to return a view not issue a redirect to the user based on certain errors that could occur from my application, I want to handle the errors + log them inside my base controller, I do not want the error to propagate up to my Global.asax - Application_Error() method as I want this method to handle any other errors inside my app e.g. user enters a bogus URL, has anyone found a way around this? NOTE: I have left my commented code as I had a workaround for some issues, this also shows

How to achieve a dynamic controller and action method in ASP.NET MVC?

青春壹個敷衍的年華 提交于 2019-12-17 08:49:11
问题 In Asp.net MVC the url structure goes like http://example.com/{controller}/{action}/{id} For each "controller", say http://example.com/blog, there is a BlogController. But my {controller} portion of the url is not decided pre-hand, but it is dynamically determined at run time, how do I create a "dynamic controller" that maps anything to the same controller which then based on the value and determines what to do? Same thing with {action}, if the {action} portion of my url is also dynamic, is

How do I configure ASP.NET MVC routing to hide the controller name on a “home” page?

杀马特。学长 韩版系。学妹 提交于 2019-12-17 07:34:27
问题 Following on from this question: ASP.NET MVC Routing with Default Controller I have a similar requirement where my end user doesn't want to see the controller name in the url for the landing or "home page" for their application. I have a controller called DeviceController which I want to be the "home page" controller. This controller has a number of actions and I'd like to use URL's like the following: http://example.com -> calls Index() http://example.com/showdevice/1234 -> calls ShowDevice

Jquery dialog partial view server side validation on Save button click

早过忘川 提交于 2019-12-17 07:34:16
问题 I have a table that displays data. Each row of table has Edit button. When edit button is clicked a jquery dialog appears with Form for editing the user info and save and cancel button . The form is nothing but a partial view buttons are part of partial view. <button name="button" class="button" id="editCurrentRow" onclick="EditCurrentRow(@item.ID); return false;"> $("#editResult").dialog({ title: 'Edit Admin', autoOpen: false, resizable: false, height: 500, width: 600, show: { effect: 'drop'