0

I'm really new to MVC and I am trying to be clever..

I have one View that displays one question but offers various methods of responding;

@using Microsoft.AspNet.Identity
@model Template.Models.Question

@{
   ViewBag.Title = "View question";
   var qtype = Model.QuestionTypeId;
   ViewBag.Number = Model.Id - 7;
   Html.BeginForm("ViewQuestion", "Question", FormMethod.Post, new { @class = "form-horizontal", role = "form" }); 
}
<div>
<h4>Question #@ViewBag.Number</h4>
<hr />
<h1> @Model.Question1</h1>
</div>
@Html.AntiForgeryToken()
<div class="form-group">
@switch (qtype)
    {
    case 1:
    // Textbox
        @Html.TextArea("Answer", new { @class = "form-control", rows = "4", col = "5" });
        break;
    case 2:
    //      Dropdown
    <select class="form-control" id="Answer">
        @foreach (var item in Model.QuestionOptions.OrderBy(o => o.QuestionOptionRanking))
        {
            <option value="@item.QuestionOption1">@item.QuestionOption1</option>

        }
    </select>
break;
case 3:
//     Checkbox
   <div class="checkbox">
    @foreach (var item in Model.QuestionOptions.OrderBy(o=> o.QuestionOptionRanking))
{
        <input type="checkbox" name="Answer" value="@item.QuestionOption1" />  @item.QuestionOption1 <br />
                }
   </div>
   break;

case 4:
//      Radio buttons
foreach (var item in Model.QuestionOptions.OrderBy(o => o.QuestionOptionRanking))
{
<div class="radio">
    <label>
        <input type="radio" name="Answer" value="@item.QuestionOption1" />
        @item.QuestionOption1
    </label>
</div>
}

break;
}

</div>
<input type="hidden" name="QuestionId" value="@Model.Id" />
<div class="form-group">
  <div class="col-md-offset-2 col-md-10">
     <input type="submit" class="btn btn-default" value="Answer" />
  </div>
</div>

My ViewModel;

    public class ResponseViewModel
{
    [Required]
    public string UserId { get; set; }
    [Required]
    public int QuestionId { get; set; }
    [Required(ErrorMessage = "Please answer the question before submitting")]
    public string Answer { get; set; }
    [Required]
    public string Source { get; set; }
    [Required]
    public string Status { get; set; }
    [Required]
    public System.DateTime DateStamp { get; set; }
    public Nullable<int> Duplicate { get; set; }
    public virtual Question Questions { get; set; }
    public object SelectedValue { get; set; }
    public virtual ICollection<QuestionOption> QuestionOptions { get; set; }

}

And finally my action;

     [HttpPost]
        public ActionResult ViewQuestion([Bind(Include = "QuestionId, Answer")] ResponseViewModel responseViewModel)
        {
        //var optionSelected = responseViewModel.Answer;
        //if (optionSelected == null)
        //{
        //    optionSelected = responseViewModel.Answer.SelectedValue();
        //}
        Response re = new Models.Response();
        re.Answer = responseViewModel.Answer;
        if (re.Answer == null)
        {
            re.Answer = "Work in progress!";
        //    re.Answer = responseViewModel.SelectedValue();
        //    re.Answer = int.Parse(SelectList["Question.QuestionOption1"]);
        }
            re.UserId = User.Identity.GetUserId();
            re.QuestionId = responseViewModel.QuestionId;
            re.Source = "Web";
            re.Status = "New";
            re.DateStamp = System.DateTime.Now;
            db.Responses.Add(re);
            db.SaveChanges();

        return RedirectToAction("ViewQuestion");
    }

If you examine the view you will notice an "Answer" can be a textbox, checkbox, radio buttons or dropdown. Everything works fine on display, some questions have dropdownlists, others radio button, etc. Its the posting that's got me stumped. If you notice I have not used HTML Helpers, but old fashion HTML as I kept getting errors that the types I've mentioned did not exist in my helpers.

Post works fine if its a text box, but if you look at my controller action you will see a few commented out attempts to also catch the values if it is one of the other controllers..

I thought I was being clever with my cases, but now I can't seem to write any code that will capture the selected, or checked options.

Any advice would be great!

1 Answer 1

1

I can think of one possibility off the top of my head. In MVC, you can have more than one form on a page. Instead of wrapping all of your cases in one form, make each case it's own form with it's own submit button, and have an action method for each one.

EDIT

Also in looking at your code, you're going to have a problem with the checkboxes because your Answer property is a String and if more than one checkbox is checked you're going to get an array

Sign up to request clarification or add additional context in comments.

2 Comments

This was a great help! Thanks. I ran some tests and the code works fine for Textbox and radiobutton, but I do need to find the code for the dropdown, and as you observed, I get the first answer from the checkbox, but not the rest. If I am only going to have 3 conditions, should I use an if statement in the controller or case? Is there a performance difference?
@Alex If you only have three conditions, I would just use an if statement. No need for the case with only three possibilities.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.