0

OK - from my controller/create, I want to update my 2 parts of my model:

  • JOURNAL
  • AUTHOR

The Journal\Create - will create a basic journal and also query a table of authors and populate an autocomplete input box with author names (using JQuery, tokenInput).

This works fine, so far, I have a blank form to fill in my journal details and a great way of selecting 1 or more authors.

What I get puzzled by is how I get the selected authors back to my controller as the signiture is:

[HttpPost]
public ActionResult Create(JOURNAL journal)
{
    //persist the journal object in the database ....

    but where to get the authors that were picked in the input box??? and persist those
    in the model?
}

Now, I have dabbled and found that I can use

Request.Form["authorlist"];

and that gets the ID's back (not quite sure how??), but they are all joined together without having any delimiting character so doesn't make much sense e.g.

1564654324544434344797361679

it should be

1564,6543,2454,4434,3447,9736,1679

then I can do soemthing with the data.

Anyway if you guys know how I can get back the results from my input box in a much better way, as well as using the model object to populate the database that would be great.

Here is the code below:

VIEW

@using (Html.BeginForm()){
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>JOURNAL</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.TITLE)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.TITLE)
            @Html.ValidationMessageFor(model => model.TITLE)
        </div>
        <div class="editor-label">
            Select Authors</div>
        <div class="authors">
            <div class="editor-field">
                <input type="text" id="authorlist" name="q"/>               
            </div>
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

JAVASCRIPT

  $("#authorlist").tokenInput('/author/getauthors/', {
        hintText: "Enter surname please..",
        searchingText: "Searching...",
        preventDuplicates: true,
        allowCustomEntry: true,
        highlightDuplicates: false           
    });

CONTROLLER (gets a list of authors)

public JsonResult GetAuthors(string term)
        {
            Debug.WriteLine("Term is: " + term);
            term = term.ToUpper();
            var authors = db.AUTHOR
                .Where(a => a.FULL_NAME.ToUpper().StartsWith(term))
                .Select(a => new { id = a.AUTHOR_ID, name = a.FULL_NAME });
            return Json(authors, JsonRequestBehavior.AllowGet);
        }
Vidar
  • 6,548
  • 22
  • 66
  • 96

1 Answers1

0

Place as parameter in action:

[HttpPost]
public ActionResult Create(JOURNAL journal, FormCollection collection)
{
    //persist the journal object in the database ....

    var authors = collection["q"]; // gets by name
}
karaxuna
  • 26,752
  • 13
  • 82
  • 117
  • Thats really no different to doing - Request.Form["q"]; This still results in all the ID's together, not comma seperated. – Vidar Jun 07 '12 at 13:07
  • Try collection.GetValues("q"); – karaxuna Jun 07 '12 at 14:08
  • Same result - it must be something to do with the way the JSON is formatted when I select new authors in input box, bearing in mind it is JSON data that is populating the input box. But I have no idea how to do this yet. – Vidar Jun 07 '12 at 15:15
  • Ahh, I have found that I needed to put a token delimeter in too as part of the Javascript code - so that seems to have worked. My bad. Still, I guess I could do with working out a more elegant way to send back the JSON data though... – Vidar Jun 07 '12 at 15:27