英文:
ModelState is never valid because of the id and foreign id - asp.net
问题
我有2个模型,一个是Poll
,另一个是PollOption
。在创建操作中,我想要向数据库添加一个Poll
和一个PollOptions
列表。每个PollOption
都有一个外键PollId
。
当我提交表单时,模型状态从未设置为有效,因为表格中的Id和ForeignId始终为空,即使我在HTML中放置了隐藏输入。所以为什么会发生这种情况?
模型:
public class Poll
{
[Key]
public int Id { get; set; }
[Required]
public string Question { get; set; } = "";
}
public class PollOption
{
[Key]
public int Id { get; set; }
[Required]
public string Option { get; set; } = "";
[Required]
public int PollId { get; set; }
[ForeignKey("PollId")]
[ValidateNever]
public Poll Poll { get; set; }
}
视图模型:
public class PollVM
{
public Poll Poll { get; set; }
[ValidateNever]
public List<PollOption> PollOptions { get; set; }
}
操作:
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(PollVM pollVM)
{
if(ModelState.IsValid)
{
pollVM.PollOptions.ForEach(p => p.Poll = pollVM.Poll);
_unitOfWork.PollOption.AddRange(pollVM.PollOptions);
_unitOfWork.Save();
return RedirectToAction("Index");
}
return View(pollVM);
}
视图:
@model PollVM
<div class="index-container">
<div class="subcontainer">
<h1 class="title">Poll Creator</h1>
<h2 class="subtitle">Complete the form below to create your poll!</h2>
<form method="POST" asp-action="Create">
<input asp-for="Poll.Id" type="hidden" />
<div class="form-container">
<div class="form-input">
<label asp-for="Poll.Question">Question:</label>
<input asp-for="Poll.Question" type="text" placeholder="Type the poll question here" required>
</div>
<div class="form-input">
<div class="form-input form-options">
<label>Answer Options:</label>
<div class="form-option">
<input name="PollOptions[0].Id" type="hidden" />
<input name="PollOptions[0].PollId" type="hidden" />
<input name="PollOptions[0].Option" type="text" placeholder="Option 1" required>
<img src="~/images/icons/cross.svg" alt="remove-option" width="30px" class="remove-option" onclick="removeOption(this)">
</div>
<div class="form-option">
<input name="PollOptions[1].Id" type="hidden" />
<input name="PollOptions[1].PollId" type="hidden" />
<input name="PollOptions[1].Option" type="text" placeholder="Option 2" required>
<img src="~/images/icons/cross.svg" alt="remove-option" width="30px" class="remove-option" onclick="removeOption(this)">
</div>
</div>
<button type="button" class="button side-button icon-button">Add Option <img src="~/images/icons/plus.svg" alt="add-option" width="25px"></button>
</div>
<button type="submit" class="button submit-button">Create!</button>
</div>
</form>
</div>
</div>
@section Scripts {
<script src="~/js/answer-options.js" asp-append-version="true"></script>
}
英文:
I have 2 models, one for Poll
and for PollOption
. In the create action, I want to add 1 Poll
to the database and a list of PollOptions
as well. Each PollOption
has a foreign key PollId
.
When I submit the form, the modelstate is never set to valid because the Id and ForeignId in the tables are always empty even though I put a hidden input in the HTML. So why is it happening?
Models:
public class Poll
{
[Key]
public int Id { get; set; }
[Required]
public string Question { get; set; } = "";
}
public class PollOption
{
[Key]
public int Id { get; set; }
[Required]
public string Option { get; set; } = "";
[Required]
public int PollId { get; set; }
[ForeignKey("PollId")]
[ValidateNever]
public Poll Poll { get; set; }
}
View model:
public class PollVM
{
public Poll Poll { get; set; }
[ValidateNever]
public List<PollOption> PollOptions { get; set; }
}
Action:
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(PollVM pollVM) {
if(ModelState.IsValid)
{
pollVM.PollOptions.ForEach(p => p.Poll = pollVM.Poll);
_unitOfWork.PollOption.AddRange(pollVM.PollOptions);
_unitOfWork.Save();
return RedirectToAction("Index");
}
return View(pollVM);
}
View:
@model PollVM
<div class="index-container">
<div class="subcontainer">
<h1 class="title">Poll Creator</h1>
<h2 class="subtitle">Complete the form below to create your poll!</h2>
<form method="POST" asp-action="Create">
<input asp-for="Poll.Id" type="hidden" />
<div class="form-container">
<div class="form-input">
<label asp-for="Poll.Question">Question:</label>
<input asp-for="Poll.Question" type="text" placeholder="Type the poll question here" required>
</div>
<div class="form-input">
<div class="form-input form-options">
<label>Answer Options:</label>
<div class="form-option">
<input name="PollOptions[0].Id" type="hidden" />
<input name="PollOptions[0].PollId" type="hidden" />
<input name="PollOptions[0].Option" type="text" placeholder="Option 1" required>
<img src="~/images/icons/cross.svg" alt="remove-option" width="30px" class="remove-option" onclick="removeOption(this)">
</div>
<div class="form-option">
<input name="PollOptions[1].Id" type="hidden" />
<input name="PollOptions[1].PollId" type="hidden" />
<input name="PollOptions[1].Option" type="text" placeholder="Option 2" required>
<img src="~/images/icons/cross.svg" alt="remove-option" width="30px" class="remove-option" onclick="removeOption(this)">
</div>
</div>
<button type="button" class="button side-button icon-button">Add Option <img src="~/images/icons/plus.svg" alt="add-option" width="25px"></button>
</div>
<button type="submit" class="button submit-button">Create!</button>
</div>
</form>
</div>
</div>
@section Scripts {
<script src="~/js/answer-options.js" asp-append-version="true"></script>
}
答案1
得分: 1
你忘记填写隐藏输入的值:
<input name="PollOptions[0].Id" value="@Model.PollOptions[0].Id" type="hidden"/>
英文:
You forget to fill the value for hidden input:
<input name="PollOptions[0].Id" value="@Model.PollOptions[0].Id" type="hidden"/>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论