在MVC中,在创建新的主记录后,重定向到具有多个要绑定到主记录的辅助文件的视图. [英] In MVC, After new Master Record, redirect to view for secondary file with many records to tie to master.

查看:198
本文介绍了在MVC中,在创建新的主记录后,重定向到具有多个要绑定到主记录的辅助文件的视图.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVC新手.我意识到这是非常基本的东西.

在输入主记录期间,如何获得MVC Controler动作以重定向到辅助文件的视图,该文件包含链接到主记录的多个记录.

脚手架提供了一个视图和控制器来输入主记录,但随后您将回到索引视图.我已经尝试在控制器中进行各种操作来做到这一点,但是我却大吃一惊.

桌子很简单,可容纳人员和费用.一个人,很多费用.

我要...

1.输入新的个人记录

2.单击保存

3.浏览以查看允许输入一个或多个与该人相关联的费用.

这是我在控制器中的帖子.

I''m an MVC Newbie. I realize this is very basic stuff.

During entry of a Master Record, how do I get my MVC Controler action to redirect to a View for a secondary file that holds multiple records linked to the master record.

The scaffolding provides a view and controller to enter the master record but then takes you back to the Index View. I have tried various things in the controller to do this, but I''m striking out.

The tables are very simple and hold people and charges. One person, many charges.

I need to...

1. Enter new person record

2. Click Save

3. Navigate to view that allows entry of one or many charges that will be linked to the person.

Here is my Post in the controller.

[HttpPost]
public ActionResult Create(dataOffender offender)
{
    if (ModelState.IsValid)
    {
        db.dataOffenders.Add(offender);
        db.SaveChanges();
        return RedirectToAction("Index");

        <<<<<<<<<TODO:redirect to my charges "CreateCharge" view. 

    }

    return View(offender);
}




感谢您愿意提供的任何帮助.




Thanks for any help you are willing to give.

推荐答案

您可以采用多种方法.我将展示POCO路线,因为这就是我接近MVC的方式.

首先,我要确保dataOffender类具有指向Charges对象的1:M链接:
There are a number of approaches that you can take. I''m going to show the POCO route, since that''s how I approach MVC.

First I would make sure that the dataOffender class has a 1:M link to the charges object:
public class dataOffender
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity), Display(AutoGenerateField = false)]
    public int Id {get; set; }

   /* other fields */

   public virtual ICollection<charge> Charges { get; set; }
</charge>



我要确保在charges类中定义了外键关系:



I would make sure that the foreign key relationship is defined in the charges class:

public class charge
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity), Display(AutoGenerateField = false)]
    public int Id { get; set; }

    [Display(AutoGenerateField = false), ForeignKey("dataOffender")]
    public int OffenderId { get; set; }

    /* other fields */

   public virtual dataOffender Offender { get; set; }

    public charge() {}

    public charge(int offenderId)
    {
         OffenderId = offenderId;
    }
}



这将建立您的关系并允许所有可口的参照完整性.现在,在您的控制器上,您将要添加一个操作,该操作将允许您创建费用.



This will establish your relationship and allow all that tasty referential integrity. Now on your controller you''ll want to add an action that will allow you to create charges.

OffenderController.cs
[HttpPost]
        public ActionResult Create(dataOffender offender)
        {
            if (ModelState.IsValid)
            {
                db.dataOffenders.Add(offender);
                db.SaveChanges();
                TempData["offenderId"] = offender.Id;
                return RedirectToAction("CreateCharge","Charge");                            
            }
 
            return View(offender);
        }

ChargeController.cs
public ActionResult CreateCharge()
{
    return View((TempData["offenderId"] != null) ? new charge(TempData["offenderId"]) : new charge());
}

[HttpPost]
public ActionResult CreateCharge(charge new_charge)
{
    if(ModelState.IsValid)
    {
        db.charges.Add(new_charge);
        db.SaveChages();
        return RedirectToAction("Index");
    }
}



我只是未经测试就将它们放在一起,因此对任何错误我深表歉意.



I just threw this together without testing, so I apologize for any errors.


这篇关于在MVC中,在创建新的主记录后,重定向到具有多个要绑定到主记录的辅助文件的视图.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆