简体   繁体   English

动态添加视图ASP.NET MVC

[英]Dynamically Add Views ASP.NET MVC

I've had a look over a couple of the other questions on the site and cant find anything that exactly answers what I'm looking to do. 我浏览了网站上的其他几个问题,但找不到任何能完全满足我要执行的操作的问题。 To help I'll give a bit of background. 为了帮助我,我会给您一些背景知识。

I've recently started experimenting with ASP.NET and MVC4. 我最近开始尝试使用ASP.NET和MVC4。 As my first real attempt at building something useful I am building a web application that will allow me to record and track my workouts in the gym. 作为构建有用东西的第一次真正尝试,我正在构建一个Web应用程序,该应用程序将允许我记录和跟踪在健身房中的锻炼情况。 I have got the basis of all my models/controllers/views etc. The part I am having trouble with is the actual layout of the page to record workouts. 我已经掌握了所有模型/控制器/视图等的基础。我遇到的问题是记录锻炼的页面的实际布局。 Each Workout is made up of a list of Sets (The sets contain information like Exercise, Weight, No of Repetitions etc.... Now the way I want this to work on the WebApp is for a user to be able to hit a button for adding a set. This will then load aa section below without a page re-load that allows them to enter information about that set. They can hit the same button again to record a second set so on and so forth.... 每个锻炼都由一组列表组成(这些集合包含诸如锻炼,体重,重复次数等信息。...现在,我希望此方法在WebApp上的工作方式是使用户能够按一下按钮然后添加下面的一个部分,而无需重新加载页面即可让他们输入有关该集合的信息。他们可以再次按下同一按钮来记录第二个集合,依此类推。

They will then hit a save button and I need to loop through each "Set" that has been added and save the information to a database. 然后,他们将点击保存按钮,我需要遍历已添加的每个“设置”,并将信息保存到数据库中。

I think it should be possible, just not exactly sure how to achieve it. 我认为应该有可能,只是不完全确定如何实现它。 The way I would do it in a standard Windows Application is using UserControls, I am thinking maybe Partial Views in ASP.NET and MVC? 我在标准Windows应用程序中执行此操作的方式是使用UserControls,我想也许是ASP.NET和MVC中的部分视图?

Any ideas guys? 有想法吗?

Any more questions let me know. 还有其他问题让我知道。

You said "without a page re-load". 您说“没有页面重新加载”。 You can do that by using AJAX. 您可以使用AJAX做到这一点。 It sounds like you have to do much fundamental education to reach your goals. 听起来您必须要做很多基础教育才能达到目标。 I want to suggest you to work into JavaScript and understand the difference between client-side and server-side in the world of web development. 我想建议您使用JavaScript,并了解Web开发领域中客户端和服务器端之间的区别。 In addition it is important to know the behaviour of HTTP (requests, responses, etc..). 此外,了解HTTP的行为(请求,响应等)也很重要。

After that, you are able to load content from the server asynchronously using and ajax request. 之后,您可以使用和ajax请求从服务器异步加载内容。 You can do that easily using jQuery. 您可以使用jQuery轻松做到这一点。 Example here: 这里的例子:

$.ajax({
  type: 'post',
  url: '/Controller/Action',
  data: {CustomerName: 'James', AdditionalData: ... },
  dataType: 'JSON',
         success: function() {

           // do anything after request success

         }

});

With JavaScript you are able to create an communication between server and client and manipulate the DOM of the client. 使用JavaScript,您可以在服务器和客户端之间创建通信并操纵客户端的DOM。

We did something similar with MVC5, maybe you can figure something out from here. 我们用MVC5做过类似的事情,也许您可​​以从这里弄清楚一些事情。

We used AJAX and PartialViews, at first the page loads we load a table with some initial content, and an Add Option button. 我们使用了AJAX和PartialViews,首先页面加载时,我们加载了具有一些初始内容的表以及一个Add Option按钮。 When the user hits the button, we increment the current count and add another row to the table via an action which returns a partial view. 当用户点击按钮时,我们将增加当前计数,并通过返回部分视图的操作将另一行添加到表中。

<script>
  var currentCount = 1;
  $(document).ready(function () {
    $("#AddOptionButton").click(function () {
      var CountryOptions = {};
      CountryOptions.url = "AddOption";
      CountryOptions.type = "POST";
      CountryOptions.datatype = "text/html";
      CountryOptions.data = JSON.stringify({count: currentCount });
      CountryOptions.contentType = "application/json";
      CountryOptions.success = function (html) {
        $("#attributesList tr:last").after(html);
        currentCount = currentCount + 1;
      };
      $.ajax(CountryOptions);
    });
  });
</script>

<table id="attributesList">
  <tr>
    <th>#</th>
    <th>Name</th>
  </tr>
  <tr>
    <td>1.</td>
    <td><input type="text" name="optionInput[0]" value="Option 1" /></td>                    
  </tr>
</table>

<input type="button" name="AddOptionButton" id="AddOptionButton" value="Add 1 More" />
<input type="submit" value="Submit Form" />

Our partial view will get the current count as input and returns something like below 我们的局部视图将获得当前计数作为输入,并返回如下所示的内容

<tr id="NewRow1">
  <td>2.</td>
  <td><input type="text" name="optionInput[1]" value="New Option Value"/></td>
</tr>

Note that we are getting an array of optionInput when the form is submitted. 请注意,提交表单时,我们将获得一个optionInput数组。 We can easily save the data by looping through the inputs. 我们可以通过遍历输入轻松地保存数据。

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

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