简体   繁体   English

如何从视图获取数据到控制器mvc4?

[英]How to get data from view to controller mvc4?

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. 请帮我解决这个问题。

您必须添加一个form元素,并且要发送回服务器的所有值都应放在输入元素中,如果不想在文本字段中显示它,则可以使用隐藏字段。

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: 在控制器中,您可以使用FormCollection获取值,并可以从中提取值,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM