我希望我能看到更多的类和代码,因为您没有正确的设置。
我根据您提供的内容重新创建了一些有效的方法。我为此示例创建了一个MVC 3项目。
视图/共享/_Layout.cshtml
<!DOCTYPE html><html><head> <title>@ViewBag.Title</title> <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /> <script src="@Url.Content("~/scripts/jquery-1.7.1.min.js")" type="text/javascript"></script></head> <body> @RenderBody() </body></html>视图/共享/_Partial.cshtml
@model RazorListTest.Models.AnswerScheme<table>@for (int i = 0; i < Model.Answers.Count; i++) { <tr> <td> @Html.HiddenFor(model => Model.Answers[i].IsMissing) @Html.TextBoxFor(model => Model.Answers[i].Value, new { @class = "inputValue" }) </td> <td> @Html.TextBoxFor(model => Model.Answers[i].Text, new { @class = "inputAnswer" }) </td> <td><span data-answer-scheme-id="@Model.Id" data-answer-id="@Model.Answers[i].Id" >x</span></td> </tr>}</table>型号/AnswerDisplayItem.cs
using System.Collections.Generic;namespace RazorListTest.Models{ public class AnswerDisplayItem { public bool IsMissing { get; set; } public string Text { get; set; } public string Value { get; set; } public string Id { get; set; } } public class AnswerScheme { public List<AnswerDisplayItem> Answers { get; set; } public string Id { get; set; } public AnswerScheme() { Answers = new List<AnswerDisplayItem>(); } }}主页/Index.cshtml
@model RazorListTest.Models.AnswerScheme @using (Html.BeginForm(null, null, FormMethod.Get, new { name="formAnswerScheme", id = "formAnswerScheme"})) { {Html.RenderPartial("_Partial");} <div> <input type="button" value="Click me" id="btnClick"/> </div> <div id="divAnswerSchemeContainer"> </div> }<script type="text/javascript"> $("#btnClick").click(function () { $.ajax({ url: 'Home/AddAnswer', type: 'POST', dataType: 'json', data: $("#formAnswerScheme").serialize(), success: function (data) { console.log(data); $("#divAnswerSchemeContainer").html(data); }, error: function (xhr, textStatus, exceptionThrown) { alert(JSON.parse(xhr.responseText)); } }); });</script>控制器/HomeController.cs
using System.Collections.Generic;using System.Web.Mvc;using RazorListTest.Models;namespace RazorListTest.Controllers{ public class HomeController : Controller { public ActionResult Index() { AnswerScheme a = new AnswerScheme(); a.Id = "1cd14b08-ce3b-4671-8cf8-1bcf69f12b2d"; List<AnswerDisplayItem> adi = new List<AnswerDisplayItem>(); AnswerDisplayItem a1 = new AnswerDisplayItem(); a1.IsMissing = false; a1.Text = "Ja"; a1.Value = "0"; a1.Id = "1234"; AnswerDisplayItem a2 = new AnswerDisplayItem(); a2.IsMissing = false; a2.Text = "Nein"; a2.Value = "1"; a2.Id = "5678"; adi.Add(a1); adi.Add(a2); a.Answers = adi; return View(a); } [HttpPost] public JsonResult AddAnswer(AnswerScheme answerScheme) { return Json("the list is in the Model."); } }}


