MVC–Hooking up my randomizer

Structurally, I have 3 projects, the site, the EF database and the randomizer. The randomizer uses entities from the EF database, so all is good on that front.

So, first cut, add a method to my Controller called Randomize, which takes in one of the EF entities:

public ActionResult Randomize(TableB tableB) { /*...*/ }

I’ve even given it an [HttpPost] attribute, now, I’m not anyway sure this is right, time will tell right? Anyhews, simple method:

[HttpPost]
public ActionResult Randomize(TableB tableB)
{
    var res = Randomizer.Randomize(tableB);
    return View(tableB);
}

I have no intention of this doing anything at present, I have breakpoints on both the lines to see what I am going to get..

First flaw – need to create the View, – Right click job…

Nope, that wasn’t it, is it the HttpPost?

Yes-ish

The way I’m linking was again naive… I was doing:

@Html.ActionLink("Randomize", "Randomize", new { tableB = Model})

but this only gives me a null TableB, so, best to follow the suit of things like the default edit link and switch that to an id…

@Html.ActionLink("Randomize", "Randomize", new { id = Model.Id})

aaand the appropriate method update…

public ActionResult Randomize(int id)
{
    var tb = db.TableB.SingleOrDefault(c => c.Id == id);
    if (tb == null)
        return RedirectToAction("Index");

    var rand = Randomizer.Randomize(tb);

    return View(tb);
}

as it happens, I’d also neglected to put any data in to TableB to be randomized, which did cause some minor inconveniences..

Breakpoint wise – we’re now looking good, the stuff is working but I will play with the view things in a bit. I noted that TableB wasn’t populated because I’d forgotten to add another method to allow it to be populated, so I got a bit distracted with that…

Print | posted @ Wednesday, October 26, 2011 8:36 AM

Comments on this entry:

No comments posted yet.

Post A Comment
Title:
Name:
Email:
Comment:
Verification: