ASP.NET MVC - 生成的表单输入的模型绑定

时间:2017-04-08 08:28:44

标签: asp.net-mvc asp.net-mvc-5 model-binding

我有一个应用程序,用户可以根据需要定义任意数量的问题,这些问题必须由应用程序中的其他用户回答,每个问题的项目都有两个属性(IdValue)但是问题是,当我发布表单时,默认的模型绑定将在表单上发布所有输入。

以下是模型:

public class Question
{
    public Guid Id { get; set; }
    public string Title { get; set; }
    public IList<QuestionItem> Items { get; set; }
}

public class QuestionItem
{
    public Guid Id { get; set; }
    public string Title { get; set; }
    public FieldType FieldType { get; set; }
}

public enum FieldType
{
    RadioButton,
    CheckBox,
    Text
}

public class QuestionResponse
{
    public Guid Id { get; set; }
    public ICollection<ResponseValue> Values { get; set; }
}

public class ResponseValue
{
    public Guid Id { get; set; }
    public string Value { get; set; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var questions = new List<Question>
        {
            new Question { Title = "Question One", Items = new List<QuestionItem>
            {
                new QuestionItem { Id = Guid.NewGuid(), Title = "Item One", FieldType = FieldType.RadioButton },
                new QuestionItem { Id = Guid.NewGuid(), Title = "Item Two", FieldType = FieldType.RadioButton },
                new QuestionItem { Id = Guid.NewGuid(), Title = "Item Two", FieldType = FieldType.RadioButton },
            } },
            new Question { Title = "Question Two", Items = new List<QuestionItem>
            {
                new QuestionItem { Id = Guid.NewGuid(), Title = "Item One", FieldType = FieldType.CheckBox },
                new QuestionItem { Id = Guid.NewGuid(), Title = "Item Two", FieldType = FieldType.CheckBox },
                new QuestionItem { Id = Guid.NewGuid(), Title = "Item Two", FieldType = FieldType.CheckBox },
            } },
            new Question { Title = "Question Three", Items = new List<QuestionItem>
            {
                new QuestionItem { Id = Guid.NewGuid(), Title = "Item One", FieldType = FieldType.RadioButton },
                new QuestionItem { Id = Guid.NewGuid(), Title = "Item Two", FieldType = FieldType.RadioButton },
                new QuestionItem { Id = Guid.NewGuid(), Title = "Item Two", FieldType = FieldType.RadioButton },
            } }
        };
        return View(questions);
    }

    [HttpPost, ValidateAntiForgeryToken]
    public ActionResult Index(QuestionResponse model)
    {
        var form = this.Request.Form;
        return RedirectToAction("Index");
    }
}

查看:

@using TestFormGenerator.Models
@model IList<Question>

@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    for (var i = 0; i <= Model.Count - 1; i++)
    {
        <div class="panel panel-default col-md-6">
            <div class="panel-heading">
                <h3 class="panel-title">@Model[i].Title</h3>
            </div>
            <div class="panel-body">
                <div class="form-horizontal">
                    @for (var j = 0; j <= Model[i].Items.Count - 1; j++)
                    {
                        <div class="form-group">
                            <label class="col-sm-2 control-label">@Model[i].Items[j].Title</label>
                            <div class="col-sm-5">

                                @switch (Model[i].Items[j].FieldType)
                                {
                                    case FieldType.RadioButton:

                                        @Html.RadioButton(string.Format("Values[{0}].Id", i), Model[i].Items[j].Id)

                                        break;
                                    case FieldType.CheckBox:

                                        @Html.CheckBox(string.Format("Values[{0}].Id", j), Model[i].Items[j].Id)

                                        break;
                                    case FieldType.Text:
                                        break;
                                    default:
                                        throw new ArgumentOutOfRangeException();
                                }

                            </div>
                            <div class="col-sm-5">

                                @Html.TextBox(string.Format("Values[{0}].Value", i))

                            </div>
                        </div>
                    }
                </div>
            </div>
        </div>
    }

    <div class="pull-left">
        <button class="btn btn-success" type="submit">
            Save
        </button>
    </div>
}

Here's the working example以防您需要测试。

任何想法?

0 个答案:

没有答案