MVC :: Updating Multiple Objects In A View?

Jan 25, 2010

I have a view that is based on a view model. This model has a single property called Entity, of type ServiceProvider, which is a Linq2SQL object. This object has relationships to 2 other objects Address and ServiceProviderGroup. In a HTML view I have textboxes that allow the properties of the Entity property to be changed and also the properties of the Address object related to it.

E.g: ViewModel.Entity.Code, ViewModel.Entity.Name, ViewModel.Entity.Address.City, etc, etc.

There is also a selectlist that will allow the Entity.GroupId (int) property to be changed. This defines a relationship to the ServiceProviderGroup class which is also accessible via a Group propery. (standard linq2sql back references)

The Edit[Post] method accepts an instance of this view model and all properties (on the Entity, Entity.Address properties) are set as expected. I then lookup the original object in the database and I also bring back the original address and group data.

E.G: entity => entity.Code == viewModel.Code && entity.Address.Id == viewModel.Address.Id && entity.Group.Id == viewModel.Entity.GroupId (I'm not looking at the code, but it's something similar to this)

Either way the original Entity, Entity.Address, etc, come back from the database. I then apply the changes from the view model to the original object using UpdateModel and everything is updated as expected. But, if I try to change Entity.GroupId, i.e. change the relationship on the entity to another ServiceGroupProvider object, I get an exception in the generated code of the ServiceProvider object.

This happens in the GroupId property of the ServiceProvider class (viewModel.Entity). When it reaches the line if(!this.HasLoadedOrAssignedValue) it enters the "if" statement and throws an exception. When the data is first read from the database it skips this part. But, when UpdateModel attempts to apply the new Entity.GroupId value it throws the exception.

View 2 Replies


Similar Messages:

MVC :: Pattern For EF 4.0 CRUD / Transactions When Updating Multiple Objects?

Nov 17, 2010

I am looking for an accepted pattern for using MVC 2 and EF. Especially I am interested on how to implement updates. Where should the ObjectContext be created, where should the queries be, how to pass a model object from the controller and have it persisted with minimum robust code. I also want to be able to use transactions when updating multiple objects.

I used Linq in an asp.net application (code behind model). With EF I was hearing it was much more n-tier friendly, but I don't find it any different, rather than people talking about POCO which to me, all it means is satisfying the desire of those that want to have control of every piece of code being created.

View 6 Replies

MVC :: - View - Passing Multiple Objects To The View And Selecting Data From The Lists?

May 17, 2010

I have a view which takes two objects: booking and list of reasons for canceling that booking.I have two classes: clsBooking, clsBookingCancelationReason I can read both objects in my view - no problems there. After I read the objects, I display the booking details and I generate a list of cancelation reasons in the following way:

[Code]....

The code above generates a list of cancelation reasons.How do I pick up the selected ReasonId from the list?
I need to generate a link that will contain the bookingId and the selected reason for canceling the booking.I can get the bookingId out easily since it's stored in the Model...but how do I go about the selected ReasonId?

View 5 Replies

Forms Data Controls :: Details View - Blocking Multiple Clicks When Updating?

Mar 12, 2010

I have a problem that is driving me insane. I have a simple detailsview that operates as you would expect a detailsview to do - the only difference being that in the updated event I send an email to various people to advise of detail changes (I have conditional testing to do this).

The problem is that if the user goes crazy with left clicking the update button, email recipients can receive multiple duplicate emails.

I have looked all over the net and seen many varied solutions for this problem - mainly using javascript - but none of them seem to be able to block multiple clicks without disabling or hiding the update button - which obviously causes issues with client side validation in the event that information is missing or incorrect.

View 4 Replies

.net - Session Objects Not Updating?

Apr 27, 2010

I set a session object at one juncture in my code:

Session("my_name") = "Dave"

Later in my code I give the user a chance to update this object:

Session("my_name") = TextBox1.Text

I reload my page and display a little hello statement like this:

Label1.Text = "Hello" & CStr(Session("my_name"))

The result is: "Hello Dave" no matter what I change Session("my_name") too.

EDIT: Here is the a full code-behind I wrote up to demonstrated:

Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ExpiresAbsolute = DateTime.Now.AddMonths(-1)
If Page.IsPostBack = False Then
Session("my_name") = "Dave"
End If
Label1.Text = CStr(Session("my_name"))
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Session("my_name") = TextBox1.Text
End Sub

View 3 Replies

C# - Cached Objects Update Automatically With The Object Updating?

Jul 2, 2010

I found some code on the web and it threw me off. Look at the code below. You will notice only when the Hits == 1, does the cache get added. After that, the cache object isn't updated. It begs the question, does the object when updated, update the cache as well automatically? The answer here would make me remove some code in some of my classes.

public static bool IsValid( ActionTypeEnum actionType )
{
HttpContext context = HttpContext.Current;
if( context.Request.Browser.Crawler ) return false;
string key = actionType.ToString() + context.Request.UserHostAddress;
var hit = (HitInfo)(context.Cache[key] ?? new HitInfo());
if( hit.Hits > (int)actionType ) return false;
else hit.Hits ++;
if( hit.Hits == 1 )
context.Cache.Add(key, hit, null, DateTime.Now.AddMinutes(DURATION),
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Normal, null);
return true;
}

I would only guess that I would need to add the lines after the if statement:

if( hit.Hits == 1 )
context.Cache.Add(key, hit, null, DateTime.Now.AddMinutes(10),
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Normal, null);
else if (hit.Hits > 1)
{context.Cache.Remove(key);
context.Cache.Add(key, hit, null, DateTime.Now.AddMinutes(10),
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Normal, null);
}
Found the code at the bottom of the page here: http://www.codeproject.com/KB/aspnet/10ASPNetPerformance.aspx?msg=2809164

View 1 Replies

Entity Framework. Updating EntityCollection Using Disconnected Objects Via Navigation Property?

Apr 22, 2010

I have a question, much liket this unanswered one. I'm trying to work with the entity framework, and having a tough time getting my foreign tables to update. I have something basically like this in the DB:

Incident (table):

-ID
-other fields

Responses (table):

-FK:Incident.ID
-other fields

And and entities that match:

Incident (entity)
-ID
-Other fields
-Responses (EntityCollection of Responses via navigation property)

Each Incident can have 0 or more responses.

In my Webpage, I have a form to allow the user to enter all the details of an Incident, including a list of responses. I can add everything to the database when a new Incident is created, however I'm having difficulty with editing the Incident.

When the page loads for edit, I populate the form and then store the responses in the viewstate. When the user changes the list of responses (adds one, deletes one or edits one). I store this back into the viewstate. Then when the user clicks the save button, I'd like to save the changes to the Incident and the Responses back to the DB. I cannot figure out how to get the responses from the detached viewstate into the Incident object so that they can be updated together.

Currently when the user clicks save, I'm getting the Incident to edit from the db, making changes to the Incident's fields and then saving it back to the DB. However I can't figure out how to have the detached list of responses from the viewstate attach to the Incident. I have tried the following without success:

Clearning the Incident.Responses collection and adding the ones from the viewstate back in:

Incident.Responses.Clear()
for each objResponse in Viewstate("Responses")
Incident.Responses.add(objResponse)
next
Creating an EntityCollection from my list and then assiging that to the Incident.Responses
Incident.Responses = EntityCollectionFromViewstateList
Iterating through the responses in Incident.Response and assigning the corresponding object from viewstate:
for each ObjResponse in Incident.Responses
objResponse = objCorrespondingModifedResonseFromViewState
Next

These all fail, I'd like to be able to merge the changes into the Inicdent object so that when the BLL calls SaveChanges on the changes to both the Incident and Responses will happen at the same time.

View 1 Replies

MVC :: Partial View Updating Parent VIew ViewData?

Oct 27, 2010

"When a partial view is instantiated, it gets its own copy of the ViewDataDictionary object that is available to the parent view. The partial view therefore has access to the data of the parent view. However, if the partial view updates the data, those updates affect only the partial view's ViewData object. The parent view's data is not changed."Is there a common way around this? For instance I have a View containing two Partial Views (User Controls) that source the same data from the Parents ViewData. The first Partial View is able to update (add/delete) certain data from the Parents View Data, however the second Partial View sources the same data but obviously isn't reflecting the changes owing to the above MSDN statement.

View 4 Replies

Forms Data Controls :: Updating Multiple Table / Multiple Row?

Feb 21, 2011

I've scenerio where i've two dropdownlist box (one dependent on other, displays handset brand name and on selection displays the respective model name), two combolistbox which is bounded to their respective datasource(Displays the fault and accessories).

Now i've to update the three tables booking(Handsetid, Brand,model), accessories(handsetId,AcesoriesID) and fault(handsetID,faultID). Ive to update multiple rows in Accessories and fault.

These controls are present in datagridview EditTemplate

View 4 Replies

How To Return Two Types Of Objects In MVC View

Mar 2, 2011

I have say class/ojbect one which I am returning as details. But then there is a second class that is related to the first that I want to list (and have an option to look at the details). How do I get return both of those?

Example is like in an item as the main piece but there may be details or other items that are related to the main item. I will select them with a linq query but then how do I return them to the same view?

Do I need to have some sort of partial or other form to display the second object type? I don't see where returning the view I can return both.

My thought for right now is to create a new class that contains both of the object types but I know there has to be a better way.

View 3 Replies

MVC :: Passing A Collection Of Objects To The View?

Apr 16, 2010

I'm trying to pass a list of items from a LINQ query to a View, but I'm having trouble figuring out how to resolve a type issue. Here's the error:

The model item passed into the dictionary is of type 'System.Data.Linq.DataQuery`1[Project.Models.Diagnostic]' but this dictionary requires a model item of type 'Project.Models.Diagnostic'.

The issue seems to originate with this code from my Controller:

[Code]....

I tried changing the IQueryable<Diagnostic> to var and got the same error. Also played around with something like:

[Code]....

But then VS2008 says that diagnostics could never be null, and that's may not the best (or even valid) way to do it.

View 1 Replies

MVC :: Passing Objects Between View And Controller?

Sep 2, 2010

I would like to pass List<SelectListItem> (not selected item but the whole "List" object) back to controller.

for example in my GET controller i would have this

ViewData["list"] = some select list from repository;

then on post I would like to get the list back from view..pretty much i'm trying to use ViewData["list"] as storage..this way I would use ajax to remove or add items to/from the list.

View 10 Replies

Serialized Objects In View State?

Mar 10, 2011

i have been going through the documentation of viewstate. [URL]It was mentioned that objects that can be serialized are stored in viewstate. What is the meaning of serializing of objects ??

View 2 Replies

How To Use Multiple Session Objects

Jul 23, 2010

i want to use multiple session variables for single session i know how to use for multiple session variables i dont know

View 9 Replies

MVC :: How To Update A Collection Of Objects In A View Model

Sep 14, 2010

I have been trying to design a form, which uses a view model that contains an object array. I want to set up this form so that when the submit button is clicked, all the members of the array are updated. Here is my code:

The class definition of the array element:

[Code]....

The problem is that when I clicked the submit button, none of the luSubject objects in the array Subjects got updated. But if I set up some initial values for the members of the luSubject objects, for instance, set the Name field to be "Maths" in the constructor of luSubject class, then it got displayed on the text box when the form page was loaded. This suggests that this form could display the data in the array, but cannot update it.

View 8 Replies

Which Types Of Objects Can Place In View State

Aug 27, 2010

I want to know why we must set the serializable attribute to save an object in view state.

Also, which type of objects can we store in view state?

View 5 Replies

MVC :: Load Partial View With List Of Objects?

Jul 8, 2010

I'm really new to MVC (just started a few weeks ago) and new to Ajax (never looked at it before today). I need some design / technical advice concerning Ajax, MVC, loading partial views, and posting / getting from a view. What I'd like to do it load a partial view that takes in a list of objects that have been queried from my db into an details-type view. The details view already has some other content that has been handeled and loaded from information pulled from my database and setup through some actions. It is strongly types to a model.

Below is the partial view I've created thus far:

[Code]....

And here's a portion of the Details page:

[Code]....

I'm not sure what to do for the Ajax.ActionLink. I'd like for a use to be able to type into the text box on it's left and hit "Add" (the Ajax link) and have the application add the content in the text box to the database. Should it be something else? An <input>? The Controller code piece is below:

[Code]....

So it should be adding the member into the repo / db and then reload the contents of the partialview. Instead, it's reloading the whole page and not just the partial view. (the url is changing from /project/details/5 to /project/membersection/).

View 10 Replies

MVC :: Passing A List Of Entity Objects To View From Controller?

Aug 5, 2010

I need to pass a list of Entity Objects from Controller to View but not put it in the Model context. It is the users homepage, but I am putting a list of alerts on the users home page(somewhat like facebook). So I need the User Context as the Model I pass but need to pass the Alert model as well so I can show the list of alerts.. How would I do that? The code below is what I have so far..

UserController

[Code]....

UserRepository

[Code]....

View 1 Replies

C# - How To Dynamically Build A List Of Objects In A Create View

Jul 9, 2010

I have an Employee model that has a Name and Company

I want to be able to build multiple Employees in one CREATE view. I figured I need to create an IList<Employee> and do something like:

<%= Html.TextBoxFor(m => m[0].Name) %>
<%= Html.TextBoxFor(m => m[0].Company) %>
<%= Html.TextBoxFor(m => m[1].Name) %>
<%= Html.TextBoxFor(m => m[1].Company) %>

If a user clicks on "Add another employee", I want the view to make another form for the new employee:

<%= Html.TextBoxFor(m => m[3].Name) %>
<%= Html.TextBoxFor(m => m[3].Company) %>

And continue to add form items (and increment the array index if they click on it again).

Keep in mind that I need to build the form and the list dynamically in the create view. I don't already have a populated list of Employees.

View 1 Replies

C# - Is It Possible To Map Multiple DTO Objects To A Single ViewModel Using Automapper

Jan 24, 2010

I was wondering if it is possible to map multiple DTO objects to a single ViewModel object using Automapper?

Essentially, I have multiple DTO objects and would like to display information from each on a single screen in ASP.NET MVC 2.0. To do so I would like to flatten the DTO objects (or parts of them...) into the Viewmodel and pass said viewmodel to the view. If I had one DTO this would be easy, but I've never seen it being done with multiple. Obviously there are a number of roundabout ways to do this (outside of automapper), but this is the approach that I would like to take if possible.

View 1 Replies

C# - ModelBinder With Multiple Selection And Complex Objects?

Feb 22, 2010

I'm trying to bind the selections in a multiple selection SELECT, to an IList input in the controller.

<select name="users" multiple="multiple">
<option>John</option>
<option>Mary</option>

[code]...

View 2 Replies

MVC :: Creating Multiple Complex Objects With A Webform?

Mar 3, 2010

I have a form which I use to create an issue. WHen you post the form, the form elements are that of a custom DTO. The DTO is then used on my POST method to create the new entity. That all works fine.

However, I want to add the functionality to add multiple issues at the same time from the same view. In effect, the user would click something like 'Report Additional Problem', and an extra set of form fields would appear (I will like use jquery for this, which I don't have an issue with). However, even with just leaving one set of form fields, the user should still be able to create just one issue. But if I habve my post method take a list or array of my DTO, it doesn't work.

Here is the post method before editing:

[Code]....

What I want to do is for this to take an array or list of NewIssueDto. I would then loop through each one in the list and create a new issue from it.

View 3 Replies

MVC :: Passing A List Of Objects From View To Controller Via Jquery Ajax?

Jan 22, 2010

I have a form that I don't want to post back so I need to get the form values into my controller via jquery's ajax method.Here is my test controller method:

[Code]....

When I debug the controller method and look at the lineItems parameter, it always contains 0 items. I've tried various formats for the javascipt lineItems parameter but still 0 items. I'm also open to some other way of getting these values in like the jquery form serialze method or something else.

View 2 Replies

Conditionally Show Model Objects In View Depending On Dropdownlist Value

Mar 24, 2011

Using ASP.NET MVC 2, I have a model for a view. The view has a dropdownlist of roles that is populated by the model and there is a list below that of checkboxes with a number of privileges. I want to show only specific privileges depending on the dropdownlist selected roleID and have it update the privileges everytime the dropdownlist changes. I know how to do this in web forms ASP.net with absolutely no problem but I cannot seem to figure out where to start in ASP.NET MVC. Can someone please advise me what I need to do or towards a tutorial that will show me what to do? I am a novice in MVC.

View 2 Replies

MVC :: Validating Objects Within ViewModel - Get Data From Three Tables And Display Them In 1 View

Apr 2, 2011

In my application, I need to get data from three tables and display them in 1 view. I have created a .dbml and created a ViewModel in my Models folder to deal with this. Here is my ViewModel:

[Code]....

My web application uses the Default ASP.NET Membership. So User [ u ] is of type aspnet_Users from the default Membership. This ViewModel allows all the CRUD opertation to be carried out. However on the Create and Update operations, I would LOVE some Model Validations, My question is:

1) Would the User [ u ] default validation kick into place in my ViewModel ? Cause I know by default ASP.NET Membership, has Model Validation.

2) For table SUserDetails (which I have created myself, not part of default) how do I add ViewModel validation to it for when someone does an Update or Create?

View 1 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved