Ethan Pelton
Ethan Pelton

Reputation: 1796

Trying to implement single page functionality with traditional asp.net MVC, Razor and EF

I just want to run this by some of you. the default scaffolding for an MVC 4 project in VS 2012 assumes that your list, detail, create, edit, delete pages are all seperate.

I have created one page that handles all of those functions for a primary model entity and its related entities.

The part that's got me a little concerned is how I'm segmenting this functionality.

The user interacts with the page through divs like this

<div id="createtrace">click</div>

which fires some JS that appends the div's id to the form data...

$("#createtrace").click(function () {
    $('<input />').attr('type', 'hidden')
          .attr('name', "action")
          .attr('value', "ct")
          .appendTo('#frm');
    $("#frm").submit();
});

I then have a switch in the controller that routes the action to the correct block of code, like this...

string action = Request.Form["action"].ToString();
switch (action)
{
    case "ct":
        trace trace = new trace();
        trace.bookingid = vwbooking.bookings.bookingid;
        if (ModelState.IsValid)
        {
            db.traces.Add(trace);
            db.SaveChanges();
        }
        break;
    case "ut":
        vwbooking.traces.ToList().ForEach(
            t =>
            {
                db.traces.Attach(t);
                db.Entry(t).State = EntityState.Modified;
            }
            );
        db.SaveChanges();
    break;
 }

does this seem reasonable? Am I bastardizing the routing engine? Recreating the wheel?

Upvotes: 0

Views: 353

Answers (1)

CodeCaster
CodeCaster

Reputation: 151720

You'd better just create an action per action. Make your JavaScript post to the appropriate URL, like /CreateTrace/.

Upvotes: 1

Related Questions