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);
}