栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在MVC中发布项目列表

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在MVC中发布项目列表

我希望我能看到更多的类和代码,因为您没有正确的设置。

我根据您提供的内容重新创建了一些有效的方法。我为此示例创建了一个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.");        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/483989.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号