when I use Html.HiddenFor( model => model.OwnerId ) to create a hidden field, the value assigned to that field is zero. When I use <input type="hidden" value="<%: Model.OwnerId %>" /> to add the hidden field to the form, the value is assigned correctly.
Why would Html.HiddenFor( model => model.OwnerId ) not get the correct value from the Model object? Am I supposed to load model state somehow separate from returning the model object from the action method? Here is the view:
[Code]....
The Create action method is relatively straight forward.
I'm building an MVC 2 RTM app, and I want to be able to share my model across applications. I'd *like* to be able to implement it like:ASP.NET MVC2 app (holds Views and Controllers)Class library to hold Model(s)WCF app to handle the data transactions with the models via different data stores across apps I had the MVC app working fine, but I wanted to abstract the data stuff and be able to work with the model across apps through the WCF site, so I created a class library project and moved all of the Models classes into that and set-up a WCF app, then added project references to the MVC and WCF apps that point at the class library. The idea was I can create services that take and return objects from the model via method calls across apps. It appears that everything's wired up correctly in the MVC project, so I'm passing the objects stored in the Models class library between controllers and views and everythig is compiling just fine, but for some reason the data is not being passed back from the views to the controller on POST -- all of the properties in the classes are null or empty.
When I debug the app, I can see that the values are stored in the model data dictionary but not the model object itself. What am I doing wrong? Am I on the wrong path, or missing something obvious (to some)?
I made a few changes to the DB in SQL server management studio then right clicked on the .edmx doc to get it to update. That seemed to work fine but when i compiled the app everything that referenced the EF seems to be broken.The Error list now contains the below error for all classes that used it.
The type or namespace name '' could not be found (are you missing a using directive or an assembly reference?)
I have an issue of static variable in an asp.net application. Let's say I have a server with 8 CPUs running windows 2008 R2 hosting an asp.net webbsite, nothing fancy here, no funny configuration.
Is the following statement true? at any single time, there's one and only one process is running and accepting requests, even in the recycling stage. The reason I am asking is: I have a static field in my class, and I want to make that's the only static instance in the website. I've heard in some circumstances, IIS is recylcing your application, and it will start a new process, but the old process is still working, thus I will have 2 static instances in memory, which defeat the purpose of static field.
One step further, let's assume there might be 2 instances in memory, can I assume there are at most 2 instances at any single time? And can I assume once the second instance is up, the first instance will NEVER accept new requests?
Another question: Recently I have a problem with an applicationdomain concept. Looks like if an application domain causes a memory leak, unload the domain will not release the memory (Umanaged leak). So to what extent Application Domain is isolated?
I am trying to make a post that should use the Default Model Binder functionality in ASP.NET MVC 2 but unfortunately I can't get through. When I click on the checkout button I populate a form dinamically using jQuery code and then submit this form to the server. This is the form that get submitted
This is the jQuery code that handle the submit event for the form $("#cartForm").submit(function (event) { event.preventDefault(); var form = $("#cartForm"); var panel = form.parent(); panel.parent().block(); $.ajax({ type: "post", dataType: "html", url: '<%: Url.Content("~/Order/Checkout") %>', async: false, data: form.serialize(), success: function (response, status, xml) { panel.parent().unblock(); }, error: function (response) { panel.parent().unblock(); } }); });
This is the controller action that should be get called [HttpPost] [ValidateAntiForgeryToken] public virtual ActionResult Checkout( CartModel cart ) { } And finally this is the CartModel class involved public class CartModel : BaseModel{ public int CustomerID { get; set; } public int FirmID { get; set; } public List<CartItemModel> CartItems { get; set; } public CartModel() { CartItems = new List<CartItemModel>(); } } public class CartItemModel : BaseModel { public int ServiceTypeID { get; set; } public int Quantity { get; set; } }
But the default Model Binder does not bind the web form data to a CartModel class. Using Fiddler I have been able to see that the data sent to the server is correct as you can see from the following snapshot.
I've been used to decorating data model classes with data annotation attributes, but the purist in me baulks slightly at including purely presentational attributes such as display format here. I am, however, quite happy to keep validation centric attributes here. One good reason I have to continue keeping all annotations etc. in the data model is that my view model aggregates data model classes, e.g.
my ViewModelBase.DetailItem<TEntity> property in the view model is just a reference to an entity class in my data model. If I wanted to move presentational annotations to the view model, I would have to quite radically revise my design to one where I duplicate data model properties in my view model and use an object mapping tool to populate view model objects based on data model objects.
Say I got a domain model as follows: (and my repository expect an instance of this object)
[Code]....
And a view model (which my views are based on)
[Code]....
At the moment I got it like this and have my controller action manually create a new Person object from the PersonModel object before passing it on to the repository, which does not feel right.
So I tried to have PersonModel inherit from Person and pass that to the repository (also tried casting the PersonModel to a Person object first), but that don't work out.
What's the right way to have PersonModel automatically cast to Person? I want to keep this logic as my current structure allow me to keep things really loosely coupled, with the repository layer not having a clue how it's being used.
I'm building a web application that has a particular model representing some events. Users need to be able to add N number of people to a given event. Choosing people is handled by a partial view.
I'm trying to build a menu that displays when users click "add a person" to the event. Because the event hasn't been filled out completely yet, there is nothing in the database to persist between requests.
I also have validation logic on the event page.
My proposed solution is to add the form to search or add for people on the event form itself and have a submit button that sends the values that have been added back to the server, where I can store them in ViewData or Session.
Unfortunately, doing this flags the validation.
My second solution is to load a partial view responsible for loading the UI to add/search for a person. I could add a little code on the method in the controller that returns a partial view storing the existing data in a session variable or viewdata. Trouble is, I have to submit the form to do it--again tripping the validation!!!
I'm wondering if perhaps I chose the wrong tool to do this...because in webforms, there would probably be a postback and you would just perform an operation on that postback. I'd like to avoid rewriting the application in webforms and am wondering if there are ways I'm overlooking in ASP.NET MVC.
I have recently started working on ASP.NET with MVC 2 framework, and I am facing following difficulty in validating my data,
Scenario:
In my application the view (ASPX) is divided into tabs (jQuery) and each tab's content is ViewUserControl (ASCX). The main model for the view has collection of sub models for individual tabs. I use RenderPartial method to render view user control.
[Code]....
And the user control (Tab1.ascx) refers the specific model for it,
[Code]....
Now if in my Tab1Model if I put following validation
[Code]....
In the controller ModelState.IsValid is always indicates TRUE. How do I override the validation behavior such that it as well looks the items in the collection member (which holds sub models) as well.
i am working in creating forms based wizard application.user interface is something similar of using ASP.Net wizard control. i like to create 5,6 pages with data collection forms (each page with each section) and final confirmation page. can any one tell me better way of building it using MVC. I am not sure how to maintain state across pages ( Model binding?) .
I'm wondering what the best practice is in reporting back to the browser about application or model state errors that would be displayed to the user. Can you throw an exception and handle it in the error handler of the jquery post? For example, consider this method:
[HandlerErrorWithAjaxFilter, HttpPost] public ActionResult RetrievePassword(string email) { User user = _userRepository.GetByEmail(email); if (user == null) throw new ClientException("The email you entered does not exist in our system. Please enter the email address you used to sign up."); string randomString = SecurityHelper.GenerateRandomString(); user.Password = SecurityHelper.GetMD5Bytes(randomString); _userRepository.Save(); EmailHelper.SendPasswordByEmail(randomString); if (Request.IsAjaxRequest()) return Json(new JsonAuth { Success = true, Message = "Your password was reset successfully. We've emailed you your new password.", ReturnUrl = "/Home/" }); else return View(); }
Is it correct to throw an exception in this case when the user is null? Or should I instead do this and handle it in the success handler of the jquery post: return Json(new JsonAuth { Success = false, Message = "The email you entered does not exist in our system. Please enter the email address you used to sign up.", ReturnUrl = "/Home/" });
Imagine I'm developing a webform with two dropdownlists and a submit button. The second dropdownlist depends on the choice in the first, and both are loaded dynamically using webmethods being called with javascript in de ASPX page (ajax). When I submit the form to the server, to save the data, I can't get the value of those dropdownlists. I think it is because with client AJAX no viewstate data is being generated. I need to send the data to the server and save the webform data, and identify wich data is on each webcontrol.
I have a strongly typed view. I get model passed into the view and then i assign model values to labels etc. I would then also like to set Model values programmatically on .aspx page, like:
<%= Model.someValue = "foo"; %>
and then pass that model back to controller action and than access those values. I know that I can apply values to model like these:
suppose our model has a property named "CreatedByUserId" that keeps the creator's user id
when we want to update our model, there are no need to display this field, but we should keep it's value during the update.
so, if i don't place any edit field for this property on the view, the model wouldn't have any value for CreatedByUserId property when returns to controller
to solve this, i :
1.place a hidden input in the view for these fields (which is vulnerable)
or
2.make a Get call to db and get the original CreatedByUserId value on each update (which causes additional round trips to db)
Am using a Model popup to show a grid view. From where the selected record should be posted back to a Text Box in the parent page. i loaded the grid succesfully i can post the values inside the modelpopup. but can't post the value to the text box in the parent page. and i use only server side code not client side scripting.
I am a newbie in mvc3 and i'm wondering how to use attribute like [Display(Name="")] for model that comes from an entity data model that I provide im my "Model" folder in my mvc3 project.
I didn't provide a .cs class for each of my database tables .
other words, I want the controller class render a edit form for me like :
Currently, I am working with ASP.NET MVC1 and am still learning about Model Binding and how values from a View are passed back to the Controller / Model. Specifically, I want take an existing Model, create a Table and populate the Rows of the Table, allow the user to edit some fields and pass it back. In my example, I have a Class called "Ingredient" which has 4 public accessories: Name, Barcode, Amount, and Unit. [Code]....
Or is this not possible? (Basically, I'm trying to re-create a datagrid where certain fields are editable and certain are not...)
I have a /Register [GET] Action in the controller that pre-poluates a view-model with a string and an integer and returns: return View(myModel);I can see the string being populated in the textarea and the id being populated in a hidden input. Yet when the form gets POSTed, the string value is null and the int value is 0. I verified that both values are posted to the server but the model received in the POST action is missing those values.