简体   繁体   English

Asp.net MVC-可变数量的条目

[英]Asp.net MVC - Variable amount of entries

I have a view where a user can enter a bank transaction. 我有一个视图,用户可以在其中输入银行交易。 So they select an Account, a Payee, a Category (Based on allowable categories for the selected Payee) and a Sub Category (based on the Category selection) - and a value. 因此,他们选择一个帐户,一个收款人,一个类别(基于所选收款人的允许类别)和一个子类别(基于类别选择)以及一个值。 This is done via JQuery, and is working. 这是通过JQuery完成的,并且正在运行。

However, there is a requirement to select more than one category/subcategory/amount per transaction (Split the transaction). 但是,需要为每个交易选择一个以上的类别/子类别/金额(拆分交易)。

I'd like to maybe add a 'grid' (So, a table), and a button under the category/sub category/amount section, labelled 'Add', which then adds the selected Cat/SubCat/Amount to a grid, then blanks the existing selections, ready for more to be added - allowing the user to add as many 'amounts' as needed. 我想添加一个“网格”(一个表),然后在“类别/子类别/金额”部分下添加一个标签为“添加”的按钮,然后将所选的Cat / SubCat / Amount添加到一个网格中,然后清空现有选择,准备添加更多内容-允许用户根据需要添加任意数量的“数量”。

But - how? 但是-怎么样?

On the Add button, I guess I would need to add the CategoryId, SubCategoryId and Amount to a List<>? 在添加按钮上,我想我需要将CategoryId,SubCategoryId和Amount添加到List <>? Then based on that list, build the summary grid? 然后根据该列表构建摘要网格?

I'd like there to be no flashing of the screen. 我希望屏幕不闪烁。 Would this have to be done via JQuery? 是否必须通过JQuery完成? Build the table with JQuery, and then have the list of cats/subcats/amounts sent back to the server on Form submittion? 使用JQuery构建表,然后在提交表单时将猫/子猫/金额的列表发送回服务器?

You might want to look into the jQuery tmpl plugin. 您可能需要研究jQuery tmpl插件。 This plugins allows you to build client side templates. 该插件使您可以构建客户端模板。 This is the kind of thing you might want to use on your site. 您可能想在网站上使用这种东西。

You define one template of a table row, and each time just add a new row based on that template. 您定义一个表格行的模板,每次仅基于该模板添加一个新行。 I would give each form item in the table row an extra index variable (such as name="CategoryID-1" for the first row). 我会给表行中的每个表单项额外的索引变量(例如,第一行的名称为“ CategoryID-1”)。 While inserting a new row, you just send the current index to the template, and make sure the template adds the current index to the names of the form elements. 插入新行时,只需将当前索引发送到模板,并确保模板将当前索引添加到表单元素的名称。

Then on the server side, you can do a simple query to get all the indeces. 然后在服务器端,您可以执行简单的查询来获取所有索引。

ie

int[] indices = Request.Form.AllKeys.Where(k => k.StartsWith("CategoryID-")).Select(k => Convert.ToInt32(k.Substring(11))).ToArray();

foreach (int index in indices)
{
    string cat = Request.Form["CategoryID-" + index.ToString()];
    string subcat = Request.Form["SubCategoryID-" + index.ToString()];
    //etc.

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM