gpt4 book ai didi

c# - 我如何为文本字段生成动态 ID 并从 asp.net 4 mvc3 中的模型数组获取数据

转载 作者:太空宇宙 更新时间:2023-11-03 16:27:16 25 4
gpt4 key购买 nike

这是我在这里的第一篇文章! :)

我必须将我的模型数组从我的 Controller 类返回到 View 页面。我想将数据放入文本框并为每个文本框生成动态 ID,以便通过 JavaScript 进一步使用数据(这就是我寻找动态 ID 的原因)。

型号

public partial class BhBuyerChart
{
public string Date { get; set; }
public string Quantity { get; set; }

public BhBuyerChart(string n, string d)
{
Date = n;
Quantity = d;
}
}

Controller

public ActionResult test()
{
BhBuyerChart[] model = new BhBuyerChart[7];

DataTable dt = (DataTable)ExecuteDB(ERPTask.AG_GetAllShipmentRecord, CurrentUserId);
List<BhBuyerChart> ItemList = null;
ItemList = new List<BhBuyerChart>();
int i = 0;
foreach (DataRow dr in dt.Rows)
{
model[i] = new BhBuyerChart(dr["Shipmentdate"].ToString(), dr["ShipmentQuantity"].ToString());
i++;
};

return View(model);
}

查看

第一次尝试

<div>
<% for (int i=0; i<2; i++) {%>
<%: Html.TextBoxFor(m => m[i].Quantity, new { id = "Quantity"})%> <%--value can assign from model but dnt know how to assing dynamic id --%>

<input type="text" value="<%= i %>" id="text<%=i %>"/> <%--dynamic id can be assinged dnt knw how to assing model value here in textbox --%>
<% } %>
</div>

第二次尝试

<div>
<% int i = 0; %>
<% foreach (ERP.Domain.Model.BhBuyerChart user in Model) { %>
<% i++; %>
<input type="text"; id="textbox<% i %>" ; value="<% user.Quantity %>" />
<% } %>
</div>

非常感谢大家的关注和帮助,期待您的回复!

最佳答案

我认为这应该适合您。实际上,您要做的是在 Controller 内构建一个新方法,以便您可以将更新后的值发回 Controller 。此外,您不希望 Quantity 字段具有不同的名称,因为它们不会绑定(bind) - 因此您构建的每个字段都会在 name 中显示 Quantity id 属性,当生成 HTML 时。

如果我误解了您的需求,请发表评论。

Controller

public ActionResult test()
{
BhBuyerChart[] model = new BhBuyerChart[7];

DataTable dt = (DataTable)ExecuteDB(ERPTask.AG_GetAllShipmentRecord, CurrentUserId);
List<BhBuyerChart> ItemList = null;
ItemList = new List<BhBuyerChart>();
int i = 0;
foreach (DataRow dr in dt.Rows)
{
model[i] = new BhBuyerChart(dr["Shipmentdate"].ToString(), dr["ShipmentQuantity"].ToString());
i++;
};

return View(model);
}

[HttpPost]
public ActionResult test(ICollection<BhBuyerChart> charts)
{
// This allows you to POST to the controller with the modified values

// Note that based on what you're collecting client side the charts
// will ONLY contain the Quantity value, but they will all have one.
// If you need the date you can either show a text box for that or
// even place the date inside a hidden field.
}

查看

<form method="post" action="/{controllername}/test">
...
<div>
<% for (int i=0; i<2; i++) {%>
<!-- This line will both bind the value and allow you to POST -->
<!-- this form back to the controller with the new values -->

<!-- NOTE: each control is actually going to be named the same -->
<!-- but when it's posted will post in order to the collection -->
<%: Html.TextBoxFor(m => m[i].Quantity) %>

<!-- You may or may not want this here so that you can get the -->
<!-- value of the date back to the server during a POST -->
<%: Html.HiddenFor(m => m[i].Date) %>
<% } %>
</div>
...
</form>

JavaScript

现在在 JavaScript 中,您可以使用 jQuery 简单地获取所有名为 Quantity 的元素的列表,并从该数组中使用它们。

// with this ([0].Quantity) being the template
// we'll use a simple wildcard selector to find
// all of them that end with Quantity.

var elems = $("[name$=Quantity]")

// now you have a list of the elements that you
// can use to populate the other array with -
// getting the value with a statement like...

var val = elems[0].val();

关于c# - 我如何为文本字段生成动态 ID 并从 asp.net 4 mvc3 中的模型数组获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12195059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com