where to put things my code behind would have handled?!http://forums.asp.net/t/1798486.aspx/1?where+to+put+things+my+code+behind+would+have+handled+Tue, 01 May 2012 17:19:30 -040017984864958302http://forums.asp.net/p/1798486/4958302.aspx/1?where+to+put+things+my+code+behind+would+have+handled+where to put things my code behind would have handled?! <p>Hi all,</p> <p>First off and most important -&nbsp;I am brand new to the concept of MVC. I think it is brilliant, and a breath of fresh air compaired to the traditional webform approach that&nbsp;I would have normally defaulted to.</p> <p>A question of mine is how do I handle logic that would normally have fallen to the button click on a page?</p> <p>Let me propose an example:</p> <p>I have a table of products, this has a PK of product id. I have another table called service with a PK of service_id. service has no direct link to product.. This link is handled by a table with a inrimenatal seed pk and two columns: product id and service id. every product will have an entry in the table to link it to a service.. the database is currently uneditable, I am stuck with the architecture as it is...</p> <p>I have a product controller that has create, details and edit's available and&nbsp;I am using a view model to grab the service name and display it on a view complete with all fields from product.</p> <p>What I need to do is elaborate on the edit and create pages&nbsp;as the standard (and brilliant) MVC structure wont understand my link table.&nbsp;</p> <p>basically&nbsp;I need my end user to choose&nbsp;from a picklist of service names, and for the service id of that to be stored in the&nbsp;link table along with the product id of the item being edited. In webforms&nbsp;I would have had a DDL containing all service names and just banged a load of linq behind the save button and we would have been away. I fear it wont be that simple in MVC?</p> <p>Would anyone have any advice on how&nbsp;I might handle this? btw,&nbsp;I am using MVC3 in VS2010, EF4.1, .net4 and razor..</p> <p>Many thanks</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> 2012-04-30T13:23:56-04:004958431http://forums.asp.net/p/1798486/4958431.aspx/1?Re+where+to+put+things+my+code+behind+would+have+handled+Re: where to put things my code behind would have handled?! <p>This is completely possible in MVC, but you have to approach the problem a little differently. MVC uses Models (hence the M) and you have to create models to represent your data. Even though there is no direct link in your database, you can easily create objects to model the desired behavior (a product can have multiple services, and a service can be on multiple products). Basically you would need a &quot;Product&quot; class and a &quot;Service&quot; class. If your product class has an ICollection&lt;Service&gt; inside of it you can easily display a drop down list based on this collection property.</p> <p>Then you just have to write a layer that takes this object and correct writes the data to your specified tables.<br> &nbsp;</p> <p>That's one way.</p> <p>Another way would be to use Javascript and JQuery in particular. You would have a drop down list on your page with a JQuery handler for the page load. On page load, you would then fetch data from the ServicesController (serialized to Json) and display that on your page.</p> <p></p> 2012-04-30T14:52:01-04:004959683http://forums.asp.net/p/1798486/4959683.aspx/1?Re+where+to+put+things+my+code+behind+would+have+handled+Re: where to put things my code behind would have handled?! <p>Many thanks for your answer - I am trying to blank the old webforms out of my head and concentrate now!!</p> <p>Ok, I have sorted the DDL - i was going to use json, but decided on just going for httphelper.. brill.</p> <p>So I now have a populated DDL, defaulting to the value that is already chosen and saved in the DB.. I see that the submit of changes to the edit form is handled by</p> <pre class="prettyprint">&lt;input type=&quot;submit&quot; value=&quot;Save&quot; /&gt;</pre> <p>Which&nbsp;I guess matches back to the controller here:</p> <p>&nbsp;</p> <pre class="prettyprint"> [HttpPost] public ActionResult Edit(product product) { if (ModelState.IsValid) { db.Entry(product).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(product); } </pre> <p>Is it now a case of building my logic for replacing link table entries in this action result as part of the form submit, or am I still thinking webforms and need to rethink!!?</p> <p>Many thanks (and apologies that old habits die hard!!)<br> <br> </p> <p>&nbsp;</p> 2012-05-01T10:51:08-04:004960342http://forums.asp.net/p/1798486/4960342.aspx/1?Re+where+to+put+things+my+code+behind+would+have+handled+Re: where to put things my code behind would have handled?! <p>No need to apologize, it's a learning process that we all had to go through :) Hang in there!</p> <p>The way that your form matches the controller action is by the Html.BeginForm tag (@using(Html.BeginForm())). That dynamically creates an html form tag. If called with no properties it automatically maps back to the action you used to load the page. Meaning, if you are vising the Url /model/edit/1 (model being whatever your model is) . Assuming just standard routing that will load the Edit HttpGet action method and the post will automatically map back to the Edit HttpPost. Now you can point to a different view. BeginForm actually allows you to point to other action methods and controllers (Just an FYI).</p> <p>That's a good approach to modifying your data. MVC doesn't really dictate how you &nbsp;manage your data, you can really use any technology or scheme you want. As long as you have a model representation to send data back and forth, what you do with that model is completely up to you and your specific business requirement.</p> <p>Good Luck!</p> 2012-05-01T17:19:30-04:00