单选按钮值未传输到模型 - ASP .NET Core
Radio button value is not transferred to model - ASP .NET Core
我在 asp .net 核心中有一个网页,其中包含以下输入:
@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" class="text-danger"></span>
</div>
<h5 style="padding-top:15px; padding-bottom:15px">@ViewBag.CongratsMessage</h5>
<div>
<label asp-for="GivenCommentary"></label><br />
<textarea class="form-control" asp-for="GivenCommentary"></textarea>
<span asp-validation-for="GivenCommentary" class="text-danger"></span>
</div>
<div class="form-group mb-2" style="padding-top:15px; padding-bottom:15px">
<button type="submit" class="btn btn-primary">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();
}
使用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" class="text-danger"></span>
</div>
<h5 style="padding-top:15px; padding-bottom:15px">@ViewBag.CongratsMessage</h5>
<div>
<label asp-for="GivenCommentary"></label><br />
<textarea class="form-control" asp-for="GivenCommentary"></textarea>
<span asp-validation-for="GivenCommentary" class="text-danger"></span>
</div>
<div class="form-group mb-2" style="padding-top:15px; padding-bottom:15px">
<button type="submit" class="btn btn-primary">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();
}
结果:
我在 asp .net 核心中有一个网页,其中包含以下输入:
@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" class="text-danger"></span>
</div>
<h5 style="padding-top:15px; padding-bottom:15px">@ViewBag.CongratsMessage</h5>
<div>
<label asp-for="GivenCommentary"></label><br />
<textarea class="form-control" asp-for="GivenCommentary"></textarea>
<span asp-validation-for="GivenCommentary" class="text-danger"></span>
</div>
<div class="form-group mb-2" style="padding-top:15px; padding-bottom:15px">
<button type="submit" class="btn btn-primary">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();
}
使用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" class="text-danger"></span>
</div>
<h5 style="padding-top:15px; padding-bottom:15px">@ViewBag.CongratsMessage</h5>
<div>
<label asp-for="GivenCommentary"></label><br />
<textarea class="form-control" asp-for="GivenCommentary"></textarea>
<span asp-validation-for="GivenCommentary" class="text-danger"></span>
</div>
<div class="form-group mb-2" style="padding-top:15px; padding-bottom:15px">
<button type="submit" class="btn btn-primary">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();
}
结果: