简体   繁体   English

如何在同一模型的视图中使用局部视图?

[英]How to work with a partial view within a view for same model?

I have a simple form with below functionalities. 我有一个具有以下功能的简单表格。

  1. First Part: It has a table which will display value from a database table, eg tblProfile and it contains three columns like profileid, profilename, description and in each row an edit button to edit values. 第一部分:它有一个表,该表将显示数据库表中的值,例如tblProfile ,它包含三列,如profileid, profilename, description并在每一行中都有一个用于编辑值的编辑按钮。

  2. Second Part: In the same page, I have three text boxes to enter the value to the same database table, eg tblProfile . 第二部分:在同一页面中,我有三个文本框,用于将值输入到同一数据库表中,例如tblProfile After clicking on the submit button it will insert the value into the table and will immediately display it on the table mentioned above and based on the last profileid it will show the top+1 id in the profileid textbox. 单击提交按钮后,它将值插入表中,并立即将其显示在上述表中,并根据最后一个profileidprofileid文本框中显示top + 1 id。 If any edit button clicked on the table, these three text boxes will be populated with the values. 如果在表上单击任何编辑按钮,则将使用值填充这三个文本框。 After modification they have to be updated. 修改后,必须对其进行更新。

I have created a view which uses IEnumerable<MyModel> as model so that all the profile values are displayed in the table (strongly typed view). 我创建了一个使用IEnumerable<MyModel>作为模型的视图,以便所有配置文件值都显示在表中(强类型视图)。 Now for the form part I have created a partial view which is also a strongly typed view but instead of IEnumerable<MyModel> it uses MyModel as its model type. 现在,我为表单部分创建了一个局部视图,该视图也是一个强类型化视图,但是它使用MyModel作为其模型类型,而不是IEnumerable<MyModel>

I amd confused about where I should put the Save, Update, Cancel etc. buttons: in the main view or in the partial. 我对应该将“保存”,“更新”,“取消”等按钮放在何处感到困惑:在主视图或局部视图中。 How to complete the functionality? 如何完成功能? I am using VS2010 SP1 and MVC4 and EF in the Main solution, which has one MVC project, an two folders: BusinessLayer and DataAccesLayer. 我在Main解决方案中使用VS2010 SP1,MVC4和EF,该解决方案有一个MVC项目,两个文件夹:BusinessLayer和DataAccesLayer。 The BusinessLayer folder contains separate class libraries for BO and BA, and the DataAccesLayer folder contains a class library file, and two other folders one for EF .edmx file and other one for DA class. BusinessLayer文件夹包含用于BO和BA的单独的类库,DataAccesLayer文件夹包含一个类库文件,另外两个文件夹用于EF .edmx文件,另一个用于DA类。

How can I implement this? 我该如何实施?

在此处输入图片说明

Simple approach: create one partial view for adding a new record, and another partial view for updating an existing record. 简单方法:创建一个用于添加新记录的局部视图,以及另一个用于更新现有记录的局部视图。 The reason for this approach is to separate concerns of how your post-action should handle your request. 采用这种方法的原因是分开考虑您的事后处理方式应如何处理您的请求。 Unless you add in a bool flag to tell the action whether the post command is to insert or update, it will not know what to do with the data. 除非您添加bool标志来告诉操作post命令是插入还是更新,否则它将不知道如何处理数据。 So, I suggest creating separate partials 因此,我建议创建单独的局部

// Insert partial "InsertPartialView"
@YourApp.Models.MyModel

@using (Html.BeginForm("InsertNewRecord", "Home"))
{
  // your field controls here

  <input type="submit" value="Add" />
}

// Update partial "UpdatePartialView"
@YourApp.Models.MyModel

@using (Html.BeginForm("UpdateRecord", "Home"))
{
  // your field controls here

  <input type="submit" value="Update" />
}

In your controller, use the "InsertNewRecord" action for adding new records, and "UpdateRecord" for updating an existing record. 在控制器中,使用“ InsertNewRecord”操作添加新记录,并使用“ UpdateRecord”更新现有记录。

Now initially, you can have your "insert" partial displayed in your View, so adding records requires no effort on the serve up this partial view. 现在,最初,您可以在视图中显示“插入”部分,因此添加记录不需要花费很多时间来提供此部分视图。

<div id="partialDiv">
  @Html.Partial("InsertPartialView")
</div>

Now when you want to update a record, you'll need to make a call to the server to replace your "insert" partial with the "update" partial. 现在,当您想要更新记录时,您需要调用服务器,以将“插入”部分替换为“更新”部分。 You can do this by creating an "edit" ajax-action link that accommodates each record in your table: 您可以通过创建一个“ edit” ajax-action链接来实现此目的,该链接可容纳表中的每个记录:

@Ajax.ActionLink("Edit", "GetUpdatePartial", new { id = item.profileid }, new AjaxOptions { HttpMethod = "GET", InsertionMode = InsertionMode.Replace, UpdateTargetId = "partialDiv" })

I'll explain this if you've never used the ajax link before. 如果您以前从未使用过ajax链接,我将对此进行解释。 The first three params are the text of the link, the action's name, and the id of the item -- no different than a regular Html-actionlink. 前三个参数是链接的文本,操作的名称和项目的ID,与常规的Html-actionlink相同。 The 4th param lets us define our ajax options. 第4个参数使我们可以定义ajax选项。 The "GET" method is self-explanatory -- we are not processing any data on this call, only retrieving from the db. “ GET”方法是不言自明的-我们在此调用中不处理任何数据,仅从数据库中检索。 The InsertionMode option defines how our return data should be handled in the View. InsertionMode选项定义了应如何在View中处理返回数据。 In this case, we want it to replace the current "insert" partial view, so we choose "replace". 在这种情况下,我们希望它替换当前的“插入”局部视图,因此我们选择“替换”。 Lastly, we define the element where we want our partial view to be inserted, our named "partialDiv". 最后,我们定义要在其中插入局部视图的元素,我们将其命名为“ partialDiv”。

At this point, the only thing missing is the action for our ajax-call. 在这一点上,唯一缺少的是我们的ajax调用操作。

[HttpGet]
public PartialViewResult GetUpdatePartial(int id)
{
  var record = db.tblProfile.Single(r => r.profileid == id);

  return PartialView("UpdatePartialView", record);
}

This action uses the profileid from the "edit" ajax-link to retrieve its record from the db, and then we're returning the partial view with the record as its model. 该操作使用“编辑” ajax链接中的profileid从db中检索其记录,然后返回以记录为模型的部分视图。 The result should be that this partial view will now be inserted into our "partialDiv", replacing the initial "insert" partial view. 结果应该是该局部视图现在将插入到我们的“ partialDiv”中,从而代替了初始的“插入”局部视图。

Here are the other actions if you want them: 如果需要,请执行以下其他操作:

[HttpPost]
public ActionResult InsertNewRecord(MyModel model)
{
  if (model.IsValid)
  {
    db.tblProfile.Add(model);
    db.SaveChanges();
  }

  return RedirectToAction("Index");
}

public ActionResult UpdateRecord(MyModel model)
{
  if (model.IsValid)
  {
    db.Entry(model).State = EntityState.modified;
    db.SaveChanges();
  }

  return RedirectToAction("Index");
}

Hope that helps 希望能有所帮助

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

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