c# MVC 将用户控件的多个实例添加到单个视图页面

发布于 2024-09-09 22:33:22 字数 372 浏览 4 评论 0原文

我能做什么: 我的目标是创建一个视图,其中由文本框和表格组成的用户控件根据需要添加到页面中。用户将在文本框中键入 SQL 查询,表将用于呈现结果。

我需要做什么: 我需要根据用户的请求将多个此类控件添加到页面。他们将按步骤编号。

例如:
步骤1 = sql查询得到参数列表; 步骤2=使用第一个参数返回某个值的sql查询; 步骤 3 = sql 查询,返回值的累积总和,该值将与步骤 2 中的值进行比较。

每次单击“添加”按钮时,一个新的用户控件就会出现在前一个用户控件的下方,我可以在其中创建 SQL 查询并生成我的回答。

我可以进行比较并查询数据库。没问题,我只是不确定如何在 MVC 中重用控件。

任何建议都会有帮助, 谢谢, 乔治

What I can do:
I aim to create a view where a User Control consisting of a TextBox and a Table is added to a page as needed. The user will type a SQL query in the textbox and the Table will be used to render the results.

What I need to do:
I need to add multiple of these controls to the page as the user requests them. They'll be numbered in steps.

For example:
Step 1 = sql query to get a list of parameters;
Step 2 = sql query which uses the first parameter to return a certain value;
Step 3 = sql query that returns a cumulative sum of values which will be compared to the value in step 2.

Each time the "Add" button is clicked a new user control appears beneath the previous in which I can create my SQL query and produce my answer.

I can do the comparisons and query the database. No issues, I'm just not sure how to re-use controls in MVC.

Any suggestions would be helpful,
Thanks,
George

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我也只是我 2024-09-16 22:33:22

你想做的事情既有趣又复杂。我相信您已经掌握了解决方案的逻辑,因此我将重点讨论您提出的简单问题:“[如何]将用户控件的多个实例添加到单个视图页面?”

当然,有几种方法可以做到这一点。您可以使用 javascript 克隆
模板 html 代码块,我可能会这样做,这样我就没有回发。但是,您没有提到 javascript,因此您应该简单地将对 RenderPartial 的调用包装在 foreach 循环中,并为循环提供增量值或列表。

View.aspx:

<% 
//loop over sqlQueries in list, rendering a 
//  partial for each and passing the query into the partial.
foreach(var item in Model.listOfSqlQueries)
    Html.RenderPartial("sqlBox", item);
%>     

然后,在视图中有一个按钮,用于发布到 ViewResult 并返回相同的列表和一项。

控制器.cs:

public ViewResult AddQueryBox(ViewModel viewModel) 
{
    //Add blank sql query to list
    viewModel.listOfSqlQueries.Add(new SqlQuery());

    //return calling view with newly updated viewmodel
    return View("View", viewModel);
}

What you're trying to do is interesting and complicated. I trust you have the logic of the solution well in hand, so I will focus on the simple question you posed: "[How can I] add multiple instances of a user control to a single view page?"

There are, of course, a few ways you could do this. You can use javascript to clone a
block of template html code, which is I would probably do so that I don't have a postback. However, you didn't mention javascript, so you should simply wrap your call to RenderPartial in a foreach loop and feed the loop with an incremental value or a list.

View.aspx:

<% 
//loop over sqlQueries in list, rendering a 
//  partial for each and passing the query into the partial.
foreach(var item in Model.listOfSqlQueries)
    Html.RenderPartial("sqlBox", item);
%>     

Then, have a button in the view that posts to an ViewResult and returns the same list plus one item.

Controller.cs:

public ViewResult AddQueryBox(ViewModel viewModel) 
{
    //Add blank sql query to list
    viewModel.listOfSqlQueries.Add(new SqlQuery());

    //return calling view with newly updated viewmodel
    return View("View", viewModel);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文