gpt4 book ai didi

jquery - Asp.net MVC with Entity,使用jquery将数据传递到List中。不起作用

转载 作者:行者123 更新时间:2023-12-01 00:14:49 24 4
gpt4 key购买 nike

编辑 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 中存在错误。 NameId 在您的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com