我真的覺得這應該很容易,但我認為它可能已經隨著 .Net 6 發生了變化。我可以使用輸入“name='name'”將值傳遞給我的控制器,但由于某種原因,我無法從我的模型到我的控制器中。我正在嘗試將我的行值發布到控制器。我正在使用可列舉的。我不確定我是否應該使用a。另一件事是我應該如何從模型的回圈中填充我的表格行。我想使用@Html。適用于較舊的 .net 和標簽助手是新的方式,但我無法讓任何作業來填充我的行。
<form method="post">
<div id="tblPullParts" class="container justify-content-center mt-3">
<table class="table table-striped">
<thead>
<tr>
<th></th>
<th >Order #</th>
<th >Item</th>
<th >Description</th>
<th >Quantity</th>
</tr>
</thead>
<tbody>
@foreach (var p in Model)
{
<tr>
<td><input type="radio" id="radio" name="radio"
value="@Html.DisplayFor(item => p.PartID)" /></td>
@*<td><input asp-for="Selected" type="radio" value="Selected" /></td>*@
<th scope="row">@Html.DisplayFor(item => p.PartID)</th>
<td>@Html.DisplayFor(item => p.Name)</td>
<td>@Html.DisplayFor(item => p.ItemLocation)</td>
<td>@Html.DisplayFor(item => p.PartGroup)</td>
<td>@Html.DisplayFor(item => p.Description)</td>
<td>
<input type="text" asp-for="@p.Name" id="txtNameN" />
</td>
</tr>
}
</tbody>
</table>
@*<input type="text" id="@Model[0].Name" />*@
<input type="text" id="txtName" name="txtName" value="" />
</div>
<div class="text-center">
<button type="submit" class="btn btn-lg btn-success mt-3">Start Pick</button>
</div>
</form>
[HttpPost]
public async Task<IActionResult> Index( PartVM model, string radio, string txtName)
{
if (model?.PartID != 0)
{
return View("UpdatePickQuantity", model);
}
if (!String.IsNullOrWhiteSpace(txtName))
{
}
//Request.QueryString["radio"];
var lstParts = await _ordersService.GetAllParts();
return View(lstParts);
}
uj5u.com熱心網友回復:
@Html.DisplayFor()只能顯示模型的值,如果要提交值,需要使用<input>,這里是一個簡單的demo,我hidden在你的表單中添加輸入提交值:
@model List<PartVM>
@{
int i = 0;
}
<form method="post">
<div id="tblPullParts" class="container justify-content-center mt-3">
<table class="table table-striped">
<thead>
<tr>
<th></th>
<th >Order #</th>
<th >Item</th>
<th >Description</th>
<th >Quantity</th>
</tr>
</thead>
<tbody>
@foreach (var p in Model)
{
<tr>
<td><input type="radio" id="radio" name="radio"
value="@Html.DisplayFor(item => p.PartID)" /></td>
@*<td><input asp-for="Selected" type="radio" value="Selected" /></td>*@
<th scope="row">
@Html.DisplayFor(item => p.PartID)
<input type="hidden" asp-for="@p.PartID" name="[@i].PartID">
</th>
<td>
@Html.DisplayFor(item => p.Name)
</td>
<td>
@Html.DisplayFor(item => p.ItemLocation)
<input type="hidden" asp-for="@p.ItemLocation" name="[@i].ItemLocation">
</td>
<td>
@Html.DisplayFor(item => p.PartGroup)
<input type="hidden" asp-for="@p.PartGroup" name="[@i].PartGroup">
</td>
<td>
@Html.DisplayFor(item => p.Description)
<input type="hidden" asp-for="@p.Description" name="[@i].Description">
</td>
<td>
<input type="text" asp-for="@p.Name" name="[@i].Name" id="txtNameN" />
</td>
</tr>
i ;
}
</tbody>
</table>
@*<input type="text" id="@Model[0].Name" />*@
<input type="text" id="txtName" name="txtName" value="" />
</div>
<div class="text-center">
<button type="submit" class="btn btn-lg btn-success mt-3">Start Pick</button>
</div>
</form>
控制器
[HttpPost]
public async Task<IActionResult> Index( List<PartVM> model, string radio, string txtName)
{
//.....
}
演示:

你的第二個問題可以參考這個github issue。
編輯==============================
如果只想傳遞選擇radio的行,則需要js來實作。參考這個:
創建一個視圖模型
public class PartvmViewModel
{
public int PartID { get; set; }
public string Name { get; set; }
public string ItemLocation { get; set; }
public string PartGroup { get; set; }
public string Description { get; set; }
public string? txtName { get; set; }
}
控制器
public IActionResult Display()
{
List<PartvmViewModel> viewmodel = new List<PartvmViewModel>();
//pass data from PartVM to PartvmViewModel
foreach (var item in PartVms)
{
viewmodel.Add(new PartvmViewModel()
{
PartID = item.PartID,
Description = item.Description,
ItemLocation = item.ItemLocation,
Name = item.Name,
PartGroup = item.PartGroup
});
}
return View(viewmodel);
}
[HttpPost]
public IActionResult Display([FromBody]PartvmViewModel model)
{
//because the value of radio is equal to PartID,SO i don't write it as parameter here
//.........
}
看法
@model List<PartvmViewModel>
@{
int i = 0;
}
<form method="post">
<div id="tblPullParts" class="container justify-content-center mt-3">
<table class="table table-striped">
<thead>
<tr>
<th></th>
<th >Order #</th>
<th >Item</th>
<th >Description</th>
<th >Quantity</th>
</tr>
</thead>
<tbody>
@foreach (var p in Model)
{
<tr>
<td><input type="radio" name="radio" onclick="Method(this)"
value="@Html.DisplayFor(item => p.PartID)" /></td>
@*<td><input asp-for="Selected" type="radio" value="Selected" /></td>*@
<th scope="row">
@Html.DisplayFor(item => p.PartID)
<input type="hidden" asp-for="@p.PartID" name="[@i].PartID" id="PartID">
</th>
<td>
@Html.DisplayFor(item => p.Name)
</td>
<td>
@Html.DisplayFor(item => p.ItemLocation)
<input type="hidden" asp-for="@p.ItemLocation" name="[@i].ItemLocation" id="ItemLocation">
</td>
<td>
@Html.DisplayFor(item => p.PartGroup)
<input type="hidden" asp-for="@p.PartGroup" name="[@i].PartGroup" id="PartGroup">
</td>
<td>
@Html.DisplayFor(item => p.Description)
<input type="hidden" asp-for="@p.Description" name="[@i].Description" id="Description">
</td>
<td>
<input type="text" asp-for="@p.Name" name="[@i].Name" id="txtNameN" />
</td>
</tr>
i ;
}
</tbody>
</table>
@*<input type="text" id="@Model[0].Name" />*@
<input type="text" id="txtName" name="txtName" value="" />
</div>
<div class="text-center">
<button class="btn btn-lg btn-success mt-3" onclick="Submit()">Start Pick</button>
</div>
</form>
@section Scripts
{
<script>
var Data;
function Method(obj){
this.Data = {
"PartID":$(obj).val(),
"ItemLocation" : $(obj).parent().parent().find('td:eq(2)').find(':input').val(),
"PartGroup" : $(obj).parent().parent().find('td:eq(3)').find(':input').val(),
"Description" : $(obj).parent().parent().find('td:eq(4)').find(':input').val(),
"Name" : $(obj).parent().parent().find('td:eq(4)').find(':input').val(),
}
}
function Submit(){
Data.txtName = $("#txtName").val();
$.ajax({
url : '/Home/Display',
type : 'post',
data : JSON.stringify(Data),
contentType : 'application/json'
});
}
</script>
}
我的意見===========================
實際上,在我看來,通過資料串列是可以的。因為我注意到您將 radio 的值傳遞給控制器??,所以您可以使用:
[HttpPost]
public async Task<IActionResult> Index( List<PartVM> model, string radio, string txtName)
{
var item = model.Where(i => i.PartID == int.Parse(radio)).FirstOrDefault();
//.....
}
獲取選中的行,比使用js更容易;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/483854.html
標籤:asp.net-mvc asp.net 核心 .net-6.0
上一篇:使用代理后授權停止作業
