0

I have such model

  public class TutorialModel
    {
        public string TitleWord { get; set; }

        public List<string> PossibleAnswers { get; set; }

        public List<bool> Colors { get; set; }

    }

and Controller

public class TutorialController : Controller
{
    //
    // GET: /Tutorial/
    private TutorialModel model = new TutorialModel();

    public ActionResult Index(TutorialModel paramModel)
    {
        model.TitleWord = "Go";
        model.Colors = new List<bool>();
        model.PossibleAnswers = new List<string>();
        model.PossibleAnswers.Add("1");
        model.PossibleAnswers.Add("2");
        model.PossibleAnswers.Add("3");
        model.PossibleAnswers.Add("4");

        return View(this.model);
    }

    public ActionResult PressButton(int? id)
    {

        if (model.TitleWord == model.PossibleAnswers[(int) id])
        {
            model.Colors[(int) id] = true;
        }

        return RedirectToAction("Index", model);
    }
}

with view

@{
    ViewBag.Title = "Tutorial";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<button type="button" class="btn btn-default">@Model.TitleWord</button>
<br/>
@{
    var textColor = "green"; 
}


<div class="btn-group">

        @for (int i = 0; i < Model.PossibleAnswers.Count; i++)
        {
            <button type="button"  class="btn btn-default" style="color: @textColor" onclick = " window.location.href = '@Url.Action("PressButton", "Tutorial", new {id = i})' ">@Model.PossibleAnswers[i]</button>
        }
</div>

And after I press one of the buttons I need to get my model which I use in view and button which I pressed. Info about button I got, but I have no idea how to get info sent model from view to controller. Please, help me with this problem.

Mykhalik
  • 244
  • 1
  • 4
  • 14
  • use an ajax call. see my answer here for an example http://stackoverflow.com/questions/20811513/model-in-layout-breaks-other-pages/20811744#20811744 – Matt Bodily Dec 29 '13 at 22:11

2 Answers2

0

You have to add a form element, and all the values that you want to send back to the server should be put inside input elements, you can use a hidden field if you dont want to show it inside a text field.

ryudice
  • 36,476
  • 32
  • 115
  • 163
0

In view:

@using (Html.BeginForm())
    {
    @{
        ViewBag.Title = "Tutorial";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

    <button type="button" class="btn btn-default">@Model.TitleWord</button>
    <br/>
    @{
        var textColor = "green"; 
    }


    <div class="btn-group">

            @for (int i = 0; i < Model.PossibleAnswers.Count; i++)
            {
                <button type="button" name="btn"  class="btn btn-default" style="color: @textColor" onclick = " window.location.href = '@Url.Action("PressButton", "Tutorial", new {id = i})' ">@Model.PossibleAnswers[i]</button>
            }
    </div>
    }

And in controller you can get the values using FormCollection and can extract values from it like:

 public ActionResult PressButton(FormCollection collection)
        {
    string s=collection["btn"]; //string parameter passed is the name of the field     defined in the view to a control.(btn is the name for the Button)
        }
Vishal
  • 604
  • 1
  • 12
  • 25