Click here to Skip to main content
14,325,789 members
Rate this:
Please Sign up or sign in to vote.
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?
Rate this:
Please Sign up or sign in to vote.

Solution 2

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

<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

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.....
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   

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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100