ModelState因id和foreign id而无效 – asp.net

huangapple go评论75阅读模式
英文:

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; } = &quot;&quot;;
}
public class PollOption
{
[Key]
public int Id { get; set; }
[Required]
public string Option { get; set; } = &quot;&quot;;
[Required]
public int PollId { get; set; }
[ForeignKey(&quot;PollId&quot;)]
[ValidateNever]
public Poll Poll { get; set; }
}

View model:

public class PollVM
{
public Poll Poll { get; set; }
[ValidateNever]
public List&lt;PollOption&gt; PollOptions { get; set; }
}

Action:

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(PollVM pollVM) {  
if(ModelState.IsValid)
{
pollVM.PollOptions.ForEach(p =&gt; p.Poll = pollVM.Poll);
_unitOfWork.PollOption.AddRange(pollVM.PollOptions);
_unitOfWork.Save();
return RedirectToAction(&quot;Index&quot;);
}
return View(pollVM);
}

View:

@model PollVM
&lt;div class=&quot;index-container&quot;&gt;
&lt;div class=&quot;subcontainer&quot;&gt;
&lt;h1 class=&quot;title&quot;&gt;Poll Creator&lt;/h1&gt;
&lt;h2 class=&quot;subtitle&quot;&gt;Complete the form below to create your poll!&lt;/h2&gt;
&lt;form method=&quot;POST&quot; asp-action=&quot;Create&quot;&gt;
&lt;input asp-for=&quot;Poll.Id&quot; type=&quot;hidden&quot; /&gt;
&lt;div class=&quot;form-container&quot;&gt;
&lt;div class=&quot;form-input&quot;&gt;
&lt;label asp-for=&quot;Poll.Question&quot;&gt;Question:&lt;/label&gt;
&lt;input asp-for=&quot;Poll.Question&quot; type=&quot;text&quot; placeholder=&quot;Type the poll question here&quot; required&gt;
&lt;/div&gt;
&lt;div class=&quot;form-input&quot;&gt;
&lt;div class=&quot;form-input form-options&quot;&gt;
&lt;label&gt;Answer Options:&lt;/label&gt;
&lt;div class=&quot;form-option&quot;&gt;
&lt;input name=&quot;PollOptions[0].Id&quot; type=&quot;hidden&quot; /&gt;
&lt;input name=&quot;PollOptions[0].PollId&quot; type=&quot;hidden&quot; /&gt;
&lt;input name=&quot;PollOptions[0].Option&quot; type=&quot;text&quot; placeholder=&quot;Option 1&quot; required&gt;
&lt;img src=&quot;~/images/icons/cross.svg&quot; alt=&quot;remove-option&quot; width=&quot;30px&quot; class=&quot;remove-option&quot; onclick=&quot;removeOption(this)&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;form-option&quot;&gt;
&lt;input name=&quot;PollOptions[1].Id&quot; type=&quot;hidden&quot; /&gt;
&lt;input name=&quot;PollOptions[1].PollId&quot; type=&quot;hidden&quot; /&gt;
&lt;input name=&quot;PollOptions[1].Option&quot; type=&quot;text&quot; placeholder=&quot;Option 2&quot; required&gt;
&lt;img src=&quot;~/images/icons/cross.svg&quot; alt=&quot;remove-option&quot; width=&quot;30px&quot; class=&quot;remove-option&quot; onclick=&quot;removeOption(this)&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;button type=&quot;button&quot; class=&quot;button side-button icon-button&quot;&gt;Add Option &lt;img src=&quot;~/images/icons/plus.svg&quot; alt=&quot;add-option&quot; width=&quot;25px&quot;&gt;&lt;/button&gt;
&lt;/div&gt;
&lt;button type=&quot;submit&quot; class=&quot;button submit-button&quot;&gt;Create!&lt;/button&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/div&gt;
@section Scripts {
&lt;script src=&quot;~/js/answer-options.js&quot; asp-append-version=&quot;true&quot;&gt;&lt;/script&gt;
}

答案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"/>

huangapple
  • 本文由 发表于 2023年7月20日 19:58:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729627.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定