- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
编辑 3/最终
这是使用 .NET MVC、Jquery 和编辑器模板的完整工作示例。 https://www.dropbox.com/s/t4yxbhkuowyd7va/exerciseTest.rar
编辑2
终于可以用了!感谢下面的回答。看来我需要更改 NewAnswerRow 部分行:
<input type='text' id='Answers_@(Model.AnswerId)__Answer' name='Answers[@Model.AnswerId].Answer'/>
谢谢!
编辑1:
我已遵循本指南: http://www.techiesweb.net/asp-net-mvc3-dynamically-added-form-fields-model-binding/
有趣的是,当我使用他的源代码时,它就像它应该的那样工作。但几乎一模一样。
原帖
首先感谢您阅读此消息并愿意提供帮助。我惊呆了。让我尝试解释一下我的情况。我正在尝试创建一个新练习,其中包含包含答案的列表。这些答案是使用 Jquery 即时创建的。我正在使用 EditorTemplates 阅读这些答案。一切正常,除了当我创建新的“答案”字段并按“发布”时,我的 Controller 看不到任何数据(答案)。奇怪的是。当我在答案属性中使用诸如 [required] 之类的数据注释时。我将被重定向到索引。当我再次尝试时,我的模型不再是空的。
所以基本上我的问题是,我怎样才能设法将这些答案放入我的列表中?
我为你们创建了一个新项目,以便更加清晰和具体。
我有 2 个模型,
锻炼
答案
ExerciseModel 类
[Table("Exercise")]
public class ExerciseModel
{
[Key]
public int ExerciseId { get; set; }
public string Question { get; set; }
public IList<AnswerModel> Answers { get; set; }
}
这基本上是一个简单的类。正如你们所看到的。有一个包含答案的列表。
答案模型
[Table("Answer")]
public class AnswerModel
{
[Key]
public int AnswerId { get; set; }
public string Answer { get; set; }
}
一个带有 Id 和 Answer 的简单 AnswerModel 类。
我的 Controller
public class ExerciseController : Controller
{
private readonly DataContext db = new DataContext();
//
// GET: /Exercise/
public ActionResult Index()
{
var exercise = new ExerciseModel();
exercise.Answers = GetAnswers();
return View(exercise);
}
[HttpPost]
public ActionResult Index(ExerciseModel exercisemodel)
{
//need to implement interaction with database (not yet) testing phase.
return View("PostedData", exercisemodel);
}
public ActionResult NewAnswersRow(int id)
{
var answers = new AnswerModel { AnswerId = id };
return View("partial/NewAnswersRow", answers);
}
private List<AnswerModel> GetAnswers()
{
var answerslist = new List<AnswerModel>();
answerslist.Add(new AnswerModel { AnswerId = 1, Answer = "This is a hardcoded answer"}); //normally db.Answers (for testing purposes)
return answerslist;
}
}
我的索引 View
@model exerciseTest.Models.ExerciseModel
@using (Html.BeginForm())
{
<div class="divAnswers">
<div id="container">
<div class="editor-label">
@Html.LabelFor(model => model.Question)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Question)
@Html.ValidationMessageFor(model => model.Question)
</div>
@Html.EditorFor(m => m.Answers)
</div>
<input type="button" id="btnAdd" value="Add New Item" />
</div>
<p>
<input type="submit" value="Save" />
</p>
}
<script>
$(function () {
$("#btnAdd").click(function (e) {
var itemIndex = $("#container input.iHidden").length;
console.debug("itemIndex : "+itemIndex);
e.preventDefault();
$.get("@Url.Action("NewAnswersRow", "Exercise")/"+itemIndex,function(data){
$("#container").append(data);
});
});
});
</script>
我的AnswerModel编辑器模板
@model exerciseTest.Models.AnswerModel
@{
Layout = null;
}
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<tr>
<td>
@Html.HiddenFor(x => x.AnswerId, new { @class="iHidden" })
@Html.TextBoxFor(x => x.Answer) </td>
</tr>
My NewAnswersRow.cshtml,用于使用 Jquery 即时创建新行(请参阅代码部分的索引)
@model exerciseTest.Models.AnswerModel
@{ Layout = null; }
<tr>
<td>
<input type="hidden" id="Answers_@(Model.AnswerId)__Id" class="iHidden" name='Answers[@Model.AnswerId].Id' />
<input type='text' id='Answers_@(Model.AnswerId)__AnswersText' name='Answers[@Model.AnswerId].AnswersText'/>
</td>
</tr>
以及我用于测试目的的“postedData” View
@model exerciseTest.Models.ExerciseModel
<h3>Posted data is </h3>
@foreach (var item in Model.Answers)
{
<p>@item.Answer</p>
}
我正在努力让这件事发挥作用。我必须说,在大约 5 年的时间里,我从未在 StackOverflow 上问过任何问题,因为我可以找到答案。但这一次,我需要你的帮助。
谢谢!
最佳答案
您的 NewAnswersRow 部分 View
中存在错误。 Name
和 Id
在您的 html 中是相同的,但它是在表单帖子中传递的名称。如果您查看 MVC
正在做什么,答案文本字段的 name 属性是 Answers[x].Answer
,而 Id
是答案[x].AnswerText
。
因此,将您的 name='Answers[@Model.AnswerId].AnswerText'
更改为 name='Answers[@Model.AnswerId].Answer'
以使其匹配MVC
所期待的。为我工作!
关于jquery - Asp.net MVC with Entity,使用jquery将数据传递到List中。不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15772964/
Github:https://github.com/jjvang/PassIntentDemo 我一直在关注有关按 Intent 传递对象的教程:https://www.javacodegeeks.c
我有一个 View ,其中包含自动生成的 text 类型的 input 框。当我单击“通过电子邮件发送结果”按钮时,代码会将您带到 CalculatedResults Controller 中的 Em
我有一个基本的docker镜像,我将以此为基础构建自己的镜像。我没有基础镜像的Dockerfile。 基本上,基本镜像使用两个--env arg,一个接受其许可证,一个选择在容器中激活哪个框架。我可以
假设我想计算 2^n 的总和,n 范围从 0 到 100。我可以编写以下内容: seq { 0 .. 100 } |> Seq.sumBy ((**) 2I) 但是,这与 (*) 或其他运算符/函数不
我有这个网址: http://www.example.com/get_url.php?ID=100&Link=http://www.test.com/page.php?l=1&m=7 当我打印 $_G
我想将 window.URL.createObjectURL(file) 创建的地址传递给 dancer.js 但我得到 GET blob:http%3A//localhost/b847c5cd-aa
我想知道如何将 typedef 传递给函数。例如: typedef int box[3][3]; box empty, *board[3][3]; 我如何将 board 传递给函数?我
我正在将一些代码从我的 Controller 移动到核心数据应用程序中的模型。 我编写了一个方法,该方法为我定期发出的特定获取请求返回 NSManagedObjectID。 + (NSManagedO
为什么我不能将类型化数组传递到采用 any[] 的函数/构造函数中? typedArray = new MyType[ ... ]; items = new ko.observableArray(ty
我是一名新的 Web 开发人员,正在学习 html5 和 javascript。 我有一个带有“选项卡”的网页,可以使网页的某些部分消失并重新出现。 链接如下: HOME 和 JavaScript 函
我试图将对函数的引用作为参数传递 很难解释 我会写一些伪代码示例 (calling function) function(hello()); function(pass) { if this =
我在尝试调用我正在创建的 C# 项目中的函数时遇到以下错误: System.Runtime.InteropServices.COMException: Operation is not allowed
使用 ksh。尝试重用当前脚本而不修改它,基本上可以归结为如下内容: `expr 5 $1 $2` 如何将乘法命令 (*) 作为参数 $1 传递? 我首先尝试使用“*”,甚至是\*,但没有用。我尝试
我一直在研究“Play for Java”这本书,这本书非常棒。我对 Java 还是很陌生,但我一直在关注这些示例,我有点卡在第 3 章上了。可以在此处找到代码:Play for Java on Gi
我知道 Javascript 中的对象是通过引用复制/传递的。但是函数呢? 当我跳到一些令人困惑的地方时,我正在尝试这段代码。这是代码片段: x = function() { console.log(
我希望能够像这样传递参数: fn(a>=b) or fn(a!=b) 我在 DjangoORM 和 SQLAlchemy 中看到了这种行为,但我不知道如何实现它。 最佳答案 ORM 使用 specia
在我的 Angular 项目中,我最近将 rxjs 升级到版本 6。现在,来自 npm 的模块(在 node_modules 文件夹内)由于一些破坏性更改而失败(旧的进口不再有效)。我为我的代码调整了
这个问题在这里已经有了答案: The issue of * in Command line argument (6 个答案) 关闭 3 年前。 我正在编写一个关于反向波兰表示法的 C 程序,它通过命
$(document).ready(function() { function GetDeals() { alert($(this).attr("id")); } $('.filter
下面是一个例子: 复制代码 代码如下: use strict; #这里是两个数组 my @i =('1','2','3'); my @j =('a','b','c'); &n
我是一名优秀的程序员,十分优秀!