C# 遍历Request.Forms以获取批量更新的字段值
我的视图中有一个搜索查询生成的记录列表。有些字段可以编辑,下一步就是用一个按钮/动作更新这些字段 黄色字段是已编辑的字段,而白色字段仍然与数据库表中的字段匹配。现在,当我单击update all时,我首先从DB中获取sellprice和casecost的值,然后从表单中获取值。如果值匹配,则继续,如果表单中的值已更改,则更新。我有一个datareader,它可以很好地从表/数据库中读取页面上每一行记录的值C# 遍历Request.Forms以获取批量更新的字段值,c#,asp.net-mvc,forms,C#,Asp.net Mvc,Forms,我的视图中有一个搜索查询生成的记录列表。有些字段可以编辑,下一步就是用一个按钮/动作更新这些字段 黄色字段是已编辑的字段,而白色字段仍然与数据库表中的字段匹配。现在,当我单击update all时,我首先从DB中获取sellprice和casecost的值,然后从表单中获取值。如果值匹配,则继续,如果表单中的值已更改,则更新。我有一个datareader,它可以很好地从表/数据库中读取页面上每一行记录的值 NpgsqlDataReader dr = cmd.ExecuteReader();
NpgsqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
var prod = new ProductViewModel();
prod.q_guid = Guid.Parse(dr["q_guid"].ToString());
prod.q_sellprice = Convert.ToDecimal(dr["q_sellprice"]);
prod.q_casecost = Convert.ToDecimal(dr["q_casecost"]);
/*
At this point
Need to compare dr.Read q_sellprice and q_casecost
with changed values in the fields
if != then update for that record
*/
/*Lets assign previous values (values in db) to variables*/
var previousSellprice = prod.q_sellprice;
var previousCasecost = prod.q_casecost;
var thatId = prod.q_guid;
/*Lets get current values from form/list*/
var priceList = Request.Form["item.q_sellprice"];
var costList = Request.Form["item.q_casecost"];
/*eg*/
if (previousSellprice != currentSellprice || previousCasecost != currentCasecost)
{
//lets update record with new values from view
}
-> loop move on to next row in view
我的DataReader while循环可以获得每一行的值,没有问题。当它从db中获取第一行的值时,我试图实现的是
- 我需要检查该记录表单中的当前值
- 如果它们不同,则更新当前行
- 转到“视图/上”页中的下一行
var priceList = Request.Form["item.q_sellprice"];
var costList = Request.Form["item.q_casecost"];
在我第一次运行循环时,我希望获得值10.00和8.50,进行检查,必要时进行更新。。然后移动到下一行,该行将获得3.33和8.88,执行我的检查,并在必要时对该页上的其余记录进行更新,依此类推
那么,如何循环实例中的Request.Forms,并一次获取一条记录的单个值呢
字段的视图上的cshtml为
@foreach (var item in Model)
{
<td>
€ @Html.EditorFor(modelItem => item.q_sellprice, new { name="q_sellprice" })
</td>
<td>
€ @Html.EditorFor(modelItem => item.q_casecost, new { name="q_casecost"})
</td>
@foreach(模型中的变量项)
{
€@Html.EditorFor(modeleItem=>item.q_sellprice,新的{name=“q_sellprice”})
€@Html.EditorFor(modelItem=>item.q_casecost,新的{name=“q_casecost”})
添加:更新不是问题,在表单字段中循环时从数组中获取每个记录的值是问题。这是对问题的详细描述-但据我所知,您唯一的问题是,您需要一些数据,现在是两个字符串作为操作列表(数据)表演?对吗 如果是-您可以使用Zip方法在列表中包含此类数据:
void Main()
{
string priceList = "1,2,3,4";
string costList = "2,3,4,5";
var prices = priceList.Split(new string[1] { "," }, StringSplitOptions.RemoveEmptyEntries);
var costs = costList.Split(new string[1] { "," }, StringSplitOptions.RemoveEmptyEntries);
var collectionToUpdate = prices.Zip(costs, (price, cost) => new PriceToUpdate(price, cost));
//do update in database with collectionToUpdate
}
public class PriceToUpdate
{
public PriceToUpdate(string oldPrice, string newPrice)
{
decimal priceTmp;
if (decimal.TryParse(oldPrice, out priceTmp))
{
OldPrice = priceTmp;
}
if (decimal.TryParse(newPrice, out priceTmp))
{
NewPrice = priceTmp;
}
}
public decimal OldPrice { get; set; }
public decimal NewPrice { get; set; }
}
这是一个很长的问题描述-但据我所知,你唯一的问题是,你想要一些数据,现在是两个字符串作为要执行的操作(数据)列表?正确吗 如果是-您可以使用Zip方法在列表中包含此类数据:
void Main()
{
string priceList = "1,2,3,4";
string costList = "2,3,4,5";
var prices = priceList.Split(new string[1] { "," }, StringSplitOptions.RemoveEmptyEntries);
var costs = costList.Split(new string[1] { "," }, StringSplitOptions.RemoveEmptyEntries);
var collectionToUpdate = prices.Zip(costs, (price, cost) => new PriceToUpdate(price, cost));
//do update in database with collectionToUpdate
}
public class PriceToUpdate
{
public PriceToUpdate(string oldPrice, string newPrice)
{
decimal priceTmp;
if (decimal.TryParse(oldPrice, out priceTmp))
{
OldPrice = priceTmp;
}
if (decimal.TryParse(newPrice, out priceTmp))
{
NewPrice = priceTmp;
}
}
public decimal OldPrice { get; set; }
public decimal NewPrice { get; set; }
}
我的建议是重新组织HTML并修改解析字段的方法。我过去所做的是将密钥Id(在您的示例中为
Guid
)作为输出的一部分。因此,基本视图中的结果如下所示:
如果您注意到name
属性(和Id)的前缀是q_guid
字段
public class ProductViewModelItems
{
public IList<ProductViewModel> items { get; set; } = new List<ProductViewModel>();
}
public class ProductViewModel
{
public Guid q_guid { get; set; }
public decimal q_sellprice { get; set; }
public decimal q_casecost { get; set; }
//other properties
}
现在我们构造表单,以便在post方法中将其拉回来,因此我选择了{q\u guid}{field\u name}
的格式作为
q_casecost
={q_guid}\u q_casecost
q\u sellprice
={q\u guid}\u q\u sellprice
@foreach (var item in Model.items)
{
<tr>
<td>
€ @Html.TextBoxFor(modelItem => item.q_sellprice, new { Name = string.Format("{0}_q_sellprice", item.q_guid), id = string.Format("{0}_q_sellprice", item.q_guid) })
</td>
<td>
€ @Html.TextBoxFor(modelItem => item.q_casecost, new { Name = string.Format("{0}_q_casecost", item.q_guid), id = string.Format("{0}_q_casecost", item.q_guid) })
</td>
</tr>
}
所以这里有很多事情,但是如果我们把它分解,它非常简单。首先,操作是接受一个FormCollection
对象,它是原始表单,作为NameValuePairCollection
,它将包含表单的所有键\值
public ActionResult Index(FormCollection form)
下一步是像以前一样从数据库加载视图模型。加载的顺序并不重要,因为我们将再次对其进行交互。(注意,我只是像以前一样使用静态模型)
然后,我们迭代您加载的viewmodel中的每个项,现在正在解析FormCollection
中的表单值
var caseCostStr = form.Get(string.Format("{0}_q_casecost", item.q_guid)) ?? "";
var sellPriceStr = form.Get(string.Format("{0}_q_sellprice", item.q_guid)) ?? "";
这将根据q_guid
从表单中捕获值,再次回顾我们以前使用的格式
接下来,将字符串值解析为十进制,并将其与原始值进行比较。如果任何一个值(q_sellprice
或q_casecost
)不同,我们将标记为已更改,并将其添加到updateItems
集合中
最后,我们的updateItems
变量现在包含所有发生更改的元素,您可以将这些元素提交回数据库
static ProductViewModelItems viewModel = new ProductViewModelItems()
{
items = new[]
{
new ProductViewModel { q_casecost = 8.50M, q_sellprice = 10M, q_guid = Guid.NewGuid() },
new ProductViewModel { q_casecost = 8.88M, q_sellprice = 3.33M, q_guid = Guid.NewGuid() },
new ProductViewModel { q_casecost = 9.60M, q_sellprice = 3.00M, q_guid = Guid.NewGuid() },
new ProductViewModel { q_casecost = 9.00M, q_sellprice = 5.00M, q_guid = Guid.NewGuid() },
new ProductViewModel { q_casecost = 10M, q_sellprice = 2.99M, q_guid = Guid.NewGuid() },
}
};
[HttpGet]
public ActionResult Index()
{
//load your view model from database (note mine is just static)
return View(viewModel);
}
我希望这会有所帮助。我的建议是重新组织您的HTML,并修改解析字段的方法。我过去所做的是将密钥Id(在您的示例中是
Guid
)作为输出的一部分。因此,基本视图中的结果如下所示:
如果您注意到name
属性(和Id)的前缀是q_guid
字段
public class ProductViewModelItems
{
public IList<ProductViewModel> items { get; set; } = new List<ProductViewModel>();
}
public class ProductViewModel
{
public Guid q_guid { get; set; }
public decimal q_sellprice { get; set; }
public decimal q_casecost { get; set; }
//other properties
}
现在我们构造表单,以便在post方法中将其拉回来,因此我选择了{q\u guid}{field\u name}
的格式作为
q_casecost
={q_guid}\u q_casecost
q\u sellprice
={q\u guid}\u q\u sellprice
@foreach (var item in Model.items)
{
<tr>
<td>
€ @Html.TextBoxFor(modelItem => item.q_sellprice, new { Name = string.Format("{0}_q_sellprice", item.q_guid), id = string.Format("{0}_q_sellprice", item.q_guid) })
</td>
<td>
€ @Html.TextBoxFor(modelItem => item.q_casecost, new { Name = string.Format("{0}_q_casecost", item.q_guid), id = string.Format("{0}_q_casecost", item.q_guid) })
</td>
</tr>
}
所以这里有很多事情,但是如果我们把它分解,它非常简单。首先,操作是接受一个FormCollection
对象,它是原始表单,作为NameValuePairCollection
,它将包含表单的所有键\值
public ActionResult Index(FormCollection form)
下一步是像以前一样从数据库加载视图模型。加载的顺序并不重要,因为我们将再次对其进行交互。(注意,我只是像以前一样使用静态模型)
然后,我们迭代您加载的viewmodel中的每个项,现在正在解析FormCollection
中的表单值
var caseCostStr = form.Get(string.Format("{0}_q_casecost", item.q_guid)) ?? "";
var sellPriceStr = form.Get(string.Format("{0}_q_sellprice", item.q_guid)) ?? "";
这将捕获值f