Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am not able to create an array to input into a db. Its most likely my syntax as this is my first MVC, Razor project, so if you can spot the errors and tell me it would be a huge help!

This is my view;

//    Checkbox
<div class="checkbox">
@{
    int count=1;
    string name="Answer"+1;
    foreach (var item in q.QuestionOptions.OrderBy(o => o.QuestionOptionRanking))
    {
        if (q.Answer == item.QuestionOption1)
        {
            <input type="checkbox" name="@name" id="Answer" value="@item.QuestionOption1" checked />@item.QuestionOption1<br />
                q.Answer = q.Answer[count - 1] + "," + item.QuestionOption1;
        }
        else
        {
            <input type="checkbox" name="@name" id="Answer" value="@item.QuestionOption1" />@item.QuestionOption1<br />
        }
    }
    count++;
 }
</div>


While this does seem to cycle through to populate the page, I cannot get it to create an array for Answer to input into my db.

This is the input controller;

// POST: /Questions/ViewQuestion/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ViewQuestion([Bind(Include = "QuestionId, Answer, UserId")] ResponseViewModel responseViewModel)
{
    var page = System.Web.HttpContext.Current.Request["page"];
    var myType = System.Web.HttpContext.Current.Request["QuestionTypeId"];
    Response re = new Models.Response();
    if (myType != "3")
    {
        re.Answer = responseViewModel.Answer;
    }
    else
    {
        for (int i = 1; i < responseViewModel.Answer.Length; i++)
        {
            re.Answer = responseViewModel.Answer + ", " + responseViewModel.Answer[i];
        }
    }
    if (re.Answer == null)
    {
        re.Answer = "Work in progress!";
    }
    re.UserId = responseViewModel.UserId;
    re.QuestionId = responseViewModel.QuestionId;
    re.Source = "Web";
    re.Status = "New";
    re.DateStamp = System.DateTime.Now;
    db.Responses.Add(re);
    db.SaveChanges();

    return RedirectToAction("ViewQuestion/" + page);
}


Can anyone spot what I am doing wrong, please?

Many thanks
Posted
Comments
TryAndSucceed 23-Jan-14 10:36am    
While debugging, did you find any value in myType?

Just give the same name to the checkbox like for all you should use below.

HTML
<input type="checkbox" id="chkid1" name="chkName" value="1" />
<input type="checkbox" id="chkid2" name="chkName" value="2" />
<input type="checkbox" id="chkid3" name="chkName" value="3" />


Now when you are submitting the page, just use the FormCollection and use the key "chkName".

Like below

C#
public ActionResult PostThisPafe(FormCollection form)
{
    string result = form["chkName"];
    return View();
}


In that result you will get those values in a comma seperated way.

Cheers buddy.....
 
Share this answer
 
The nicest way to do what you're doing, is to build the list of answers using javascript and make an AJAX call. You can use jquery to get the list of checked ids, then build that in to a JSON string and pass it through. You don't need to use JSON if you prefer not to, a csv works, or XML.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900