我在 asp .net core 中有一個網頁,其中包含以下輸入:
@model MyProject.ViewModels.UserScoreModel
<div>
<label asp-for="GivenServiceScore"></label><br />
<input required asp-for="GivenServiceScore" type="radio" name="servScore" id="servScore" value="5" /> 5
<input required asp-for="GivenServiceScore" type="radio" style="padding-left:15px" id="servScore" name="servScore" value="4" /> 4
<input required asp-for="GivenServiceScore" type="radio" style="padding-left:15px" id="servScore" name="servScore" value="3" /> 3
<input required asp-for="GivenServiceScore" type="radio" style="padding-left:15px" id="servScore" name="servScore" value="2" /> 2
<input required asp-for="GivenServiceScore" type="radio" style="padding-left:15px" id="servScore" name="servScore" value="1" /> 1
<span asp-validation-for="GivenServiceScore" ></span>
</div>
<h5 style="padding-top:15px; padding-bottom:15px">@ViewBag.CongratsMessage</h5>
<div>
<label asp-for="GivenCommentary"></label><br />
<textarea asp-for="GivenCommentary"></textarea>
<span asp-validation-for="GivenCommentary" ></span>
</div>
<div style="padding-top:15px; padding-bottom:15px">
<button type="submit" >Submit score</button>
</div>
用戶應該給出評級并發表評論。我的問題是來自選中單選按鈕的值不會傳輸到模型。當我設定斷點并查看引數時,GivenServiceScore它總是0. 這似乎是一個小問題,但我不明白我錯過了什么。
這是模型:
public class UserScoreModel
{
[Required]
[Display(Name = "Please give a rating")]
public int GivenServiceScore { get; set; }
[Required]
[Display(Name = "Please leave a commentary")]
public String GivenCommentary { get; set; }
}
和控制器:
[HttpPost]
public async Task<IActionResult> UserReview(UserScoreModel score)
{
if(ModelState.IsValid)
{
UserReviews review = new UserReviews()
{
GivenServiceScore = score.GivenServiceScore,
GivenCommentary = score.GivenCommentary,
};
_db.UserReviews.Add(review);
_db.SaveChanges();
ViewBag.CongratsMessage = "Thank you for participating!";
return View();
}
return View();
}
uj5u.com熱心網友回復:
使用asp-for標簽后不需要添加id和name,會自動生成,如下所示:

看法:
@model WebApplication174.Models.UserScoreModel
<form method="post" asp-action="GivenServiceScore">
<div>
<label asp-for="GivenServiceScore"></label><br />
<input required asp-for="GivenServiceScore" type="radio" value="5" /> 5
<input required asp-for="GivenServiceScore" type="radio" style="padding-left:15px" value="4" /> 4
<input required asp-for="GivenServiceScore" type="radio" style="padding-left:15px" value="3" /> 3
<input required asp-for="GivenServiceScore" type="radio" style="padding-left:15px" value="2" /> 2
<input required asp-for="GivenServiceScore" type="radio" style="padding-left:15px" value="1" /> 1
<span asp-validation-for="GivenServiceScore" ></span>
</div>
<h5 style="padding-top:15px; padding-bottom:15px">@ViewBag.CongratsMessage</h5>
<div>
<label asp-for="GivenCommentary"></label><br />
<textarea asp-for="GivenCommentary"></textarea>
<span asp-validation-for="GivenCommentary" ></span>
</div>
<div style="padding-top:15px; padding-bottom:15px">
<button type="submit" >Submit score</button>
</div>
</form>
控制器:
public IActionResult Index()
{
return View();
}
[HttpPost]
public async Task<IActionResult> GivenServiceScore(UserScoreModel score)
{
if (ModelState.IsValid)
{
UserReviews review = new UserReviews()
{
GivenServiceScore = score.GivenServiceScore,
GivenCommentary = score.GivenCommentary,
};
ViewBag.CongratsMessage = "Thank you for participating!";
return View();
}
return View();
}
結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/372580.html
