【发布时间】:2019-04-04 18:13:50
【问题描述】:
我创建了一个 Razor 页面,允许用户在提交表单之前动态添加到对象模型,但是当我尝试转换为部分视图时,对象没有被添加到页面模型中。只是移动到局部视图时,不应该使用相同的代码吗?
此代码有效:
<div class="row form-group">
<div>
<input type="submit" asp-page-handler="AddQuestion" class="btn btn-info" value="Add Question" />
</div>
<div class="form-group">
@for (var i = 0; i < Model.SurveyObject.Questions.Count; i++)
{
<h4><span class="alert-info">Question ID# @Model.SurveyObject.Questions[i].QuestionId</span></h4>
<span class="hidden">
@Html.LabelFor(x => Model.SurveyObject.Questions[i].QuestionId)
@Html.EditorFor(x => Model.SurveyObject.Questions[i].QuestionId)
</span>
@Html.LabelFor(x => Model.SurveyObject.Questions[i].Question)
@Html.EditorFor(x => Model.SurveyObject.Questions[i].Question)
}
</div>
</div>
但是当转换为局部视图时它不起作用:
<div class="row form-group">
<div>
<input type="submit" asp-page-handler="AddQuestion" class="btn btn-info" value="Add Question" />
</div>
<div class="form-group">
@for (var i = 0; i < Model.SurveyObject.Questions.Count; i++)
{
@await Html.PartialAsync("_SurveyCreateQuestion", Model.SurveyObject.Questions[i]);
}
</div>
</div>
这里是支持对象。
public class Survey
{
public string SurveyName { get; set; }
public List<SurveyQuestion> Questions { get; set; }
public Survey()
{
this.SurveyName = "new survey name here";
this.Questions = new List<SurveyQuestion>();
}
}
public class SurveyQuestion
{
public int QuestionId { get; set; }
public string Question { get; set; }
public List<SurveyResponse> ResponseList { get; set; }
public SurveyQuestion() { }
public SurveyQuestion(int id)
{
this.QuestionId = id;
this.Question = "question text for id " + id;
this.ResponseList = new List<SurveyResponse>();
}
}
public class SurveyResponse
{
public int ResponseId { get; set; }
public string Response { get; set; }
public SurveyQuestion NextQuestion { get; set; }
public SurveyResponse() { }
public SurveyResponse(int id)
{
this.ResponseId = id;
this.Response = "response text for id " + id;
}
}
这是 cshml 文件。
public class SurveyCreateModel : PageModel
{
[BindProperty]
public Survey SurveyObject { get; set; }
public void OnGet()
{
}
public void OnPostAddQuestion()
{
this.SurveyObject.Questions.Add(new SurveyQuestion(this.SurveyObject.Questions.Count + 1));
}
}
还有局部视图。
@model SurveyCreateModel.SurveyQuestion
@if (Model == null)
{
<span></span>
}
else
{
<h4><span class="alert-info">Question ID# @Model.QuestionId</span></h4>
<span class="hidden">
@Html.LabelFor(x => Model.QuestionId)
@Html.EditorFor(x => Model.QuestionId)
</span>
@Html.LabelFor(x => Model.Question)
@Html.EditorFor(x => Model.Question)
}
查看呈现的 HTML 的源代码,没有局部视图的循环正在按预期创建独特的元素,例如单击两次添加问题按钮时生成的这些标签。
<label for="SurveyObject_Questions_0__QuestionId">QuestionId</label>
<label for="SurveyObject_Questions_1__QuestionId">QuestionId</label>
但是当生成标签的相同代码被移动到同一循环内的部分视图中时,html 呈现如下:
<label for="QuestionId">QuestionId</label>
部分视图的屏幕打印不起作用 - 按住添加问题但始终停留在第一个问题 - 模型未更新
代码工作的屏幕打印 - 按两次添加问题按钮并添加两个问题 - 模型正在更新
【问题讨论】:
-
请说明问题出在哪里 - 部分视图未呈现或输入数据未发布到服务器端?
-
添加了屏幕打印以显示添加问题有效和无效。
-
还添加了工作和非工作结果的渲染 html。
标签: .net post razor dynamic partial-views