Controller Post action not getting view Model ASP.NET Core Razor pages.

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

Controller Post action not getting view Model ASP.NET Core Razor pages

问题

I am creating a razor form that takes options from a list from the model and displays radio buttons and a text input. when I submit, the post method has a view model where all properties are null. how can I fix it?

我的 Razor 表单从模型中获取选项并显示单选按钮和文本输入。但是,当我提交时,POST 方法中的视图模型的所有属性都为 null。我该如何修复它?

my razor view

我的 Razor 视图

@model CreateBallotViewModel
<div class="row">
    <div class="col-md-5 offset-md-1">
        <h4 class="mb-3">@Model.elect.Question</h4>
        <div class="my-3">
            @using (Html.BeginForm(actionName: "CreateBallot", controllerName: "Account", FormMethod.Post))
            {
                @foreach (var opt in Model.options)
                {
                    <div class="form-check">
                        @Html.RadioButtonFor(expression: model => model.choice, opt.OptionName, htmlAttributes: new { @class = "form-check-input" })
                        <label class="form-check-label" for="credit">@opt.OptionName</label>
                    </div>
                }
                <div class="form-check">
                    <input name="credit" asp-for="choice" type="radio" class="form-check-input" value="">
                    <label class="form-check-label" for="credit">Neutral</label>
                </div>
                <div class="col-md-3">
                    <label for="cc-cvv" class="form-label">Tracking Number</label>
                    <input type="text" class="form-control" id="cc-cvv" placeholder="" required="" asp-for="tracking">
                    <div class="invalid-feedback">
                        Security code required
                    </div>
                </div>
                <hr class="my-4">
                <button class="btn btn-primary" type="submit">Cast Ballot</button>
            }
        </div>
    </div>
</div>

My model

我的模型

using System;
namespace electronicvotingfyp.Models
{
    public class CreateBallotViewModel
    {
        public List<ElectionOption> options { get; set; }
        public Election elect { get; set; }
        public string choice;
        public string tracking;
        //vote encrypted
        //signed vote
        //keys
    }
}

my controller with the get and post actions

我的控制器包括获取和提交操作

[Authorize]
[HttpGet]
public IActionResult CreateBallot(int ElectionId)
{
    List<ElectionOption> electionOptions = new List<ElectionOption>();
    Election election = null;
    DbContextOptions<electronicvotingfypIdentityDbContext> options = new DbContextOptions<electronicvotingfypIdentityDbContext>();
    using (var db = new electronicvotingfypIdentityDbContext(options))
    {
        election = db.Elections.Single(u => u.ElectionId == ElectionId);
        electionOptions = db.Options.Where(p => p.Election == election).ToList();
    }
    CreateBallotViewModel cbm = new CreateBallotViewModel() { options = electionOptions, elect = election };
    return View(cbm);
}

[Authorize]
[HttpPost]
public IActionResult CreateBallot(CreateBallotViewModel cbm)
{
    ElectionKeySystem keys = null;
    DbContextOptions<electronicvotingfypIdentityDbContext> options = new DbContextOptions<electronicvotingfypIdentityDbContext>();
    using (var db = new electronicvotingfypIdentityDbContext(options))
    {
        keys = db.ElectionKeys.Single(u => u.RelatedElection.ElectionId == cbm.elect.ElectionId);
    }
    PemReader pr = new PemReader(new StringReader(keys.publicKey));
    AsymmetricCipherKeyPair keyPair = (AsymmetricCipherKeyPair)pr.ReadObject();
    ElectionKeySystemContrroller ks = new ElectionKeySystemContrroller(keyPair);
    byte[] inputChoice = Encoding.UTF8.GetBytes(cbm.choice);
    byte[] inputTracking = Encoding.UTF8.GetBytes(cbm.tracking);
    byte[] encryptedChoice = ks.Encrypt(Encoding.UTF8.GetBytes(cbm.choice), 0, inputChoice.Length);
    return View(cbm);
}

I tried changing the form input which were initially html tags to Html.RadioFor. I also tried having new { enctype = "multipart/form-data" } as a parameter for Html.BeginForm. I also tried hidden fields for the properties I had set before.

我尝试将最初的 HTML 标签更改为 Html.RadioButtonFor。我还尝试将 new { enctype = "multipart/form-data" } 作为 Html.BeginForm 的参数。我还尝试使用隐藏字段来设置之前的属性。

英文:

I am creating a razor form that takes options from a list from the model and displays radio buttons and a text input. when I submit, the post method has a view model where all properties are null. how can I fix it?

my razor view

@model CreateBallotViewModel
&lt;div class=&quot;row&quot;&gt;
    &lt;div class=&quot;col-md-5 offset-md-1&quot;&gt;
        &lt;h4 class=&quot;mb-3&quot;&gt;@Model.elect.Question&lt;/h4&gt;
        &lt;div class=&quot;my-3&quot;&gt;
            @using (Html.BeginForm(actionName: &quot;CreateBallot&quot;, controllerName: &quot;Account&quot;, FormMethod.Post))
            {
                @foreach (var opt in Model.options)
                {
                    &lt;div class=&quot;form-check&quot;&gt;
                        @Html.RadioButtonFor(expression: model =&gt; model.choice, opt.OptionName, htmlAttributes: new { @class = &quot;form-check-input&quot; })
                        @*@Html.LabelFor(expression: model =&gt; @opt.OptionName, htmlAttributes: new { @class = &quot;form-check-label&quot; })
                        &lt;input name=&quot;credit&quot; asp-for=&quot;choice&quot; type=&quot;radio&quot; class=&quot;form-check-input&quot; value=&quot;@opt.OptionName&quot;&gt;*@
                        &lt;label class=&quot;form-check-label&quot; for=&quot;credit&quot;&gt;@opt.OptionName&lt;/label&gt;
                    &lt;/div&gt;

                }
                &lt;div class=&quot;form-check&quot;&gt;
                    &lt;input name=&quot;credit&quot; asp-for=&quot;choice&quot; type=&quot;radio&quot; class=&quot;form-check-input&quot; value=&quot;&quot;&gt;
                    &lt;label class=&quot;form-check-label&quot; for=&quot;credit&quot;&gt;Neutral&lt;/label&gt;
                &lt;/div&gt;
                &lt;div class=&quot;col-md-3&quot;&gt;
                    &lt;label for=&quot;cc-cvv&quot; class=&quot;form-label&quot;&gt;Tracking Number&lt;/label&gt;
                    &lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;cc-cvv&quot; placeholder=&quot;&quot; required=&quot;&quot; asp-for=&quot;tracking&quot;&gt;
                    &lt;div class=&quot;invalid-feedback&quot;&gt;
                        Security code required
                    &lt;/div&gt;
                &lt;/div&gt;
                &lt;hr class=&quot;my-4&quot;&gt;
                &lt;button class=&quot;btn btn-primary&quot; type=&quot;submit&quot;&gt;Cast Ballot&lt;/button&gt;
            }
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

My model

using System;
namespace electronicvotingfyp.Models
{
	public class CreateBallotViewModel
	{
		public List&lt;ElectionOption&gt; options { get; set; }
		public Election elect { get; set; }
		public string choice;
		public string tracking;
		//vote encrypted
		//signed vote
		//keys
	}
}

my controller with the get and post actions

 [Authorize]
        [HttpGet]
        public IActionResult CreateBallot(int ElectionId)
        {

            List&lt;ElectionOption&gt; electionOptions = new List&lt;ElectionOption&gt;();
            Election election = null;
            DbContextOptions&lt;electronicvotingfypIdentityDbContext&gt; options = new DbContextOptions&lt;electronicvotingfypIdentityDbContext&gt;();
            using (var db = new electronicvotingfypIdentityDbContext(options))
            {

                election = db.Elections.Single(u =&gt; u.ElectionId == ElectionId);
                electionOptions  = db.Options.Where(p =&gt; p.Election  == election).ToList();
            }

            CreateBallotViewModel cbm = new CreateBallotViewModel() { options = electionOptions ,elect = election };

            return View(cbm);
        }
        [Authorize]
        [HttpPost]
        public IActionResult CreateBallot(CreateBallotViewModel cbm)
        {
            ElectionKeySystem keys = null;
            DbContextOptions&lt;electronicvotingfypIdentityDbContext&gt; options = new DbContextOptions&lt;electronicvotingfypIdentityDbContext&gt;();
            using (var db = new electronicvotingfypIdentityDbContext(options))
            {

                keys = db.ElectionKeys.Single(u =&gt; u.RelatedElection.ElectionId == cbm.elect.ElectionId);
               
            }
            PemReader pr = new PemReader(new StringReader(keys.publicKey));
            AsymmetricCipherKeyPair keyPair = (AsymmetricCipherKeyPair)pr.ReadObject();
            //ElGamalPublicKeyParameters publicKeyParams = (ElGamalPublicKeyParameters)keyPair.Public;

            ElectionKeySystemContrroller ks = new ElectionKeySystemContrroller(keyPair);
            byte[] inputChoice = Encoding.UTF8.GetBytes(cbm.choice);
            byte[] inputTracking = Encoding.UTF8.GetBytes(cbm.tracking);
            byte[] encryptedChoice = ks.Encrypt(Encoding.UTF8.GetBytes(cbm.choice), 0, inputChoice.Length);

            return View(cbm);
        }
    }

I tried changing the form input which were initially html tags to Html.RadioFor. I also tried having new { enctype = "multipart/form-data" } as a parameter for Html.BeginForm. I also tried hidden fields for the properties I had set before.

答案1

得分: 1

public string Tracking { get; set; }
public string Tracking { get; set; }

英文:

Please give the getter and setter of properties

public string Tracking { get; set; }
public string Tracking { get; set; }

答案2

得分: 0

同意Nahid。

我在我的一侧重现了您的问题,没有{get; set;},我们可以在请求上下文中看到表单已经提交数据到动作,但无法绑定到模型。

顺便说一下,在您的控制器中,您有cbm.elect.ElectionId,但在您的表单代码中,您没有包含elect.ElectionId,所以您还需要将类似这样的代码放入表单中,否则cbm.elect仍将为null。

@using (Html.BeginForm(actionName: "CreateBallot", controllerName: "home", FormMethod.Post))
{
    <input style="display:none" asp-for="@Model.elect.ElectionId" />
}
英文:

Agree with Nahid.

I reproduced your issue in my side and without {get; set;}, we can see in the request context that the form already submit data to the action but can't bind to the model.

By the way, in your controller you had cbm.elect.ElectionId, but in your form code, you didn't contain elect.ElectionId, so you also need to put code like this into the form, or the cbm.elect still be null.

@using (Html.BeginForm(actionName: &quot;CreateBallot&quot;, controllerName: &quot;home&quot;, FormMethod.Post))
            {
                &lt;input style=&quot;display:none&quot; asp-for=&quot;@Model.elect.ElectionId&quot; /&gt;

Controller Post action not getting view Model ASP.NET Core Razor pages.

huangapple
  • 本文由 发表于 2023年5月22日 10:46:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302769.html
匿名

发表评论

匿名网友

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

确定