提问人:podrick 提问时间:10/28/2023 最后编辑:marc_spodrick 更新时间:10/28/2023 访问量:19
在 MVC 5 ASP.NET 中丢失了价值
Value getting lost in ASP.NET MVC 5
问:
我的网页中的值之一是没有进入控制器。
我有一个模型类:OrderDetailsModel
public class OrderDetailsModel
{
public string SerialNumber { get; set; }
public string OrderNumber { get; set; }
}
在我的 中,我有一小部分 IBM DB2 SQL 来获取值:PartsInformationController
public class PartsInformationController : Controller
{
public ActionResult DescriptionTicket(string sdsrno)
{
var daorno = "";
var sql = $@"
SELECT DAORNO
FROM {ViewBag.Library}.DLRINVAPF
WHERE DASRNO='{sdsrno}'";
using (var cmd = new OleDbCommand(sql, new OleDbConnection(_conn)))
{
cmd.Connection.Open();
using (var reader = cmd.ExecuteReader())
{
if (reader.Read())
{
daorno = $"{reader[0]}".Trim();
}
}
}
var model = new OrderDetailsModel()
{
SerialNumber = sdsrno,
OrderNumber = daorno
};
return View(model);
}
}
在我看来,我有:DescriptionTicket.cshtml
@model Website1.Models.OrderDetailsModel
@{
ViewBag.Title = "DescriptionTicket";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div id="grid-split">
<div>
<h1 class="center">PARTS SEARCH BY KEYWORD</h1>
<label>
Search By Keyword <input type="text" id="keyword" value="" /><button onclick="generateByKeyword()">Search</button>
</label>
</div>
<div id="searchResults">
</div>
</div>
<script>
function generateByKeyword() {
var partByKey = '@Url.Action( "PartByKey", "PartsInformation")';
var orderNumber =
$('#searchResults').load(partByKey,
{
Keyword: $('#keyword').val(),
Serial: @Model.OrderNumber,
dealerID: @Model.DealerID
});
}
</script>
当我单击“搜索”按钮时,两者都有它们的值,但参数为 null:keyword
dealerID
orderNumber
public ActionResult PartByKey(string keyword, string orderNumber, string dealerID)
{
var model = new PartSearchModel()
{
DealerID = dealerID,
OrderNumber = orderNumber,
};
}
我错过了什么?
除了使用 Javascript 之外,还有更好的编写方法吗?
答:
0赞
podrick
10/28/2023
#1
视图中有一个错别字。DescriptionTicket.cshtml
它应该如下:
function generateByKeyword() {
var partByKey = '@Url.Action( "PartByKey", "PartsInformation")';
$('#searchResults').load(partByKey,
{
Keyword: $('#keyword').val(),
orderNumber: @Model.OrderNumber,
dealerID: @Model.DealerID
});
}
因为控制器使用变量。orderNumber
评论