英文:
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
<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" })
@*@Html.LabelFor(expression: model => @opt.OptionName, htmlAttributes: new { @class = "form-check-label" })
<input name="credit" asp-for="choice" type="radio" class="form-check-input" value="@opt.OptionName">*@
<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();
//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: "CreateBallot", controllerName: "home", FormMethod.Post))
{
<input style="display:none" asp-for="@Model.elect.ElectionId" />
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论