首页 > 解决方案 > 如何从动态表单中获取值?

问题描述

我正在使用带有剃须刀页面的 .NET Core,并且我有一个模型作为对象列表,如下所示。谁能告诉我该怎么做?我搜索了很多论坛,似乎只有使用控制器才能做到这一点。

这是我正在使用的剃须刀 cshtml 表单:

 <form id="form" method="post" data-ajax="true" data-ajax-method="post"  >
        <h4>Performance Rating</h4>
        <div>
            <p>
                <mark>
                    @Model.CommonContents.Find(x => x.ContentId.Equals(24)).ContentText
                </mark>
            </p>
        </div>
        <div class="table-responsive">
            <table id="contentTbl" class="display nowrap table table-striped table-bordered">
                <thead>
                    <tr>
                        <th width="25%">
                            @Model.CommonContents.Find(x => x.ContentId.Equals(25)).ContentText
                        </th>
                        <th style="font-size:xx-small" width="5%">(a)<br /> @Model.CommonContents.Find(x => x.ContentId.Equals(26)).ContentText</th>
                        <th style="font-size:xx-small" width="5%">(b) <br />@Model.CommonContents.Find(x => x.ContentId.Equals(27)).ContentText</th>
                        <th style="font-size:xx-small" width="5%">(c)=(a)X(b)<br />@Model.CommonContents.Find(x => x.ContentId.Equals(28)).ContentText</th>
                        <th>@Model.CommonContents.Find(x => x.ContentId.Equals(29)).ContentText</th>
                        <th width="5%">@Model.CommonContents.Find(x => x.ContentId.Equals(30)).ContentText</th>
                    </tr>

                </thead>
                <tbody>
                    @{ var j = 0; }
                    @{ var QuestionId = 0;}

                    @for (int k = 0; k < Model.QuesAns.Count; k++)
                    {
                        j = k + 1;
                        QuestionId = Model.QuesAns[k].QuestionId;
                        <tr>
                            <td>
                                <h5>@j. @Html.DisplayFor(modelitem => Model.QuesAns[k].TitleText)</h5>
                                <p>@Html.DisplayFor(modelitem => Model.QuesAns[k].ContentText)</p>
                                <input type="hidden" name="QuestionId_@j" , id="QuestionId_@j" value="@QuestionId" />
                            </td>
                            <td align="center">
                                <lable id="Weighting_@j">@Html.DisplayFor(modelitem => Model.QuesAns[k].Weighting)</lable>%
                            </td>
                            <td align="center">
                                <select class="form-control p-0" name="Weighting_@j" id="Weighting_@j" asp-for="@Model.QuesAns[k].Weighting">
                                    <option value="0"></option>
                                    <option value="5">5</option>
                                    <option value="4.5">4.5</option>
                                    <option value="4">4</option>
                                    <option value="3.5">3.5</option>
                                    <option value="3">3</option>
                                    <option value="2.5">2.5</option>
                                    <option value="2">2</option>
                                    <option value="1.5">1.5</option>
                                    <option value="1">1</option>
                                </select>

                            </td>
                            <td align="center"><lable id="WeightingMarks_@j"></lable></td>
                            <td>
                                <textarea class="form-control" rows="10" name="Comment_@j" id="Comment_@j" asp-for="@Model.QuesAns[k].Answer" ></textarea>

                            </td>
                            <td>
                                (@Html.DisplayFor(modelitem => Model.QuesAns[k].AttachNum))
                                <a href="#" target="_blank" class="ClickAttach"><span class="k-icon k-i-attachment-45"></span></a>
                            </td>
                        </tr>
                    }
                    <tr style="background-color:burlywood">
                        <td align="right">
                            <input type="hidden" value="@Model.QuesAns.Count" name="TotalQues" id="TotalQues" />
                            <b>
                                @Model.CommonContents.Find(x => x.ContentId.Equals(31)).ContentText
                            </b>
                        </td>
                        <td>100%</td>
                        <td></td>
                        <td><label id="TotalWeightingMarks"></label></td>

                        <td></td>
                        <td></td>

                    </tr>

                </tbody>

            </table>
        </div>
        <div class="row">
            <input type="text" id="formAction" name="formAction" style="display:none">
         <input id="btnSubmit" type="submit" value="Confirmed" class="btn btn-block btn-danger" />
            </div>



        </div>
    </form>

javascript部分

$(document).ready(function () {
        $('#btnSubmit').click(function () {
            $('#formAction').val("submit");
        }); });

像这样的编码页面

[BindProperty]
    public List<VAppraisalQuestionAnswer> qa { set; get; }
 public IActionResult OnPost(string formAction)
    {
        //i want to get value from the from
        var abc = qa;  // qa is return count= 0
        return Page();
    }

这是模型

 public class VAppraisalQuestionAnswer
{



    [StringLength(50)]
    public string StaffCode { get; set; }
    [StringLength(50)]
    public string DeptCode { get; set; }
    [StringLength(10)]
    public string AppraisalPeriodCode { get; set; }
    public int QuestionId { get; set; }
    [StringLength(int.MaxValue)]
    public string TitleText { get; set; }
    [StringLength(int.MaxValue)]
    public string ContentText { get; set; }
    public int Weighting { get; set; }

    public string Answer { get; set; }
    public int AttachNum { get; set; }
    public int DspOrd { get; set; }

}

标签: asp.net-core.net-corerazor

解决方案


我已经找到了解决方案。我需要做的就是添加IFormCollectionOnPostAsync,然后IFormCollection返回数组值。

像这样:

public async Task<IActionResult> OnPostAsync(IFormCollection Form)        {

推荐阅读