如何寻找一个头,然后保存一个新成员的头部asp.net的MVC 5下? [英] How to search a head then save a new member under that head in asp.net mvc 5?

查看:342
本文介绍了如何寻找一个头,然后保存一个新成员的头部asp.net的MVC 5下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的项目的桌面应用程序这个屏幕截图,但现在我想这样做对我的asp.net的MVC 5应用程序。请看到如下所示的画面:

i have this screen shot of my project on desktop application but now i want to do it on my asp.net mvc 5 application. Please see the picture shown below:

成员

 public partial class Member
  {
   public Member()
    {
    this.Acc_Transactions = new HashSet<Acc_Transactions>();
    this.Addresses12 = new HashSet<Addresses1>();
    this.BankingDetails = new HashSet<BankingDetail>();
    this.Contacts = new HashSet<Contact>();
    this.TalentCommitments = new HashSet<TalentCommitment>();
    this.Pledges = new HashSet<Pledge>();
}

public int m_id { get; set; }
public int title_id { get; set; }
public string initial { get; set; }
public string fname { get; set; }
public string lname { get; set; }
public Nullable<System.DateTime> dob { get; set; }
public string maritial { get; set; }
public string religion { get; set; }
public string occupation { get; set; }
public string company { get; set; }
public string Note { get; set; }
public Nullable<int> Memtype_Id { get; set; }
public string employed { get; set; }
public Nullable<System.DateTime> reg_date { get; set; }
public string AccNumb { get; set; }
public string Hnumber { get; set; }
public Nullable<bool> Active { get; set; }
public string AgeGrp { get; set; }
public int h_id { get; set; }
public Nullable<int> postal_addid { get; set; }
public Nullable<int> phys_addid { get; set; }
public Nullable<int> maritialid { get; set; }
public Nullable<bool> PlndGv { get; set; }

public virtual ICollection<Acc_Transactions> Acc_Transactions { get; set; }
public virtual Addresses1 Addresses1 { get; set; }
public virtual Addresses1 Addresses11 { get; set; }
public virtual ICollection<Addresses1> Addresses12 { get; set; }
public virtual ICollection<BankingDetail> BankingDetails { get; set; }
public virtual ICollection<Contact> Contacts { get; set; }
public virtual Head Head { get; set; }
public virtual Maritial Maritial1 { get; set; }
public virtual ICollection<TalentCommitment> TalentCommitments { get; set; }
public virtual MemberType MemberType { get; set; }
public virtual ICollection<Pledge> Pledges { get; set; }
public virtual Title Title { get; set; }
    }
    }

public partial class Head
   {
    public Head()
{
    this.Addresses1 = new HashSet<Addresses1>();
    this.Members = new HashSet<Member>();
}

public int h_id { get; set; }
public string h_initials { get; set; }
public string fname { get; set; }
public string lname { get; set; }
public string Email { get; set; }
public string cell { get; set; }
public string cell2 { get; set; }
public string tel_h { get; set; }
public string tel_w { get; set; }
public string fax { get; set; }
public string h_no { get; set; }
public int title_id { get; set; }
public Nullable<bool> active { get; set; }

public virtual ICollection<Addresses1> Addresses1 { get; set; }
public virtual ICollection<Member> Members { get; set; }
public virtual Title Title { get; set; }
   }

视图模型

 public class MembersViewModel
  {
public int m_id { get; set; }
public string titles { get; set; }
public string initial { get; set; }
public string fname{ get; set; }
public string lname { get; set; }
public string email { get; set; }
public Nullable<System.DateTime> dob { get; set; }
public string maritials { get; set; }
public string religion { get; set; }
public string occupation { get; set; }
public string company { get; set; }
public string note { get; set; }
public string employed { get; set; }
public Nullable<System.DateTime> regdate { get; set; }
public string accNumb { get; set; }
public string hnumber { get; set; }
public string agegroup { get; set; }
public string plandGv { get; set; }
public string cell { get; set; }
public string tel_h { get; set; }
public int title_id { get; set; }
public string flatName { get; set; }
public string flatNo { get; set; }
public string strname { get; set; }
public string strNo { get; set; }
public string suburb { get; set; }
public string city { get; set; }
public string tel_w { get; set; }
public string fax { get; set; }
public string cell2 { get; set; }
public bool active { get; set; }
public string province { get; set; }
public string country { get; set; }
public int? postalcode { get; set; }
public string zone { get; set; }
public bool isHa { get; set; }
public int addtype { get; set; }
public int PhysAddID { get; set; }
public Nullable<int> phys_addid { get; set; }
public int h_id { get; set; }
public int maritialid { get; set; }
public int Memtype_Id { get; set; }

 }
}

index动作:

  public ActionResult Index()
    {
        HeadVM list = new HeadVM()
        {
            data = new List<Heads>()
        };
        var AllHeads = db.Heads;
        foreach (var item in AllHeads)
        {
            if (item != null)
            {
                list.data.Add(new Heads
                {
                    h_id = item.h_id,
                    fname = item.fname,
                    lname = item.lname,
                });
            }
        }
        return View(list);
    }

我的问题是,我想第一个搜索头的详细信息,然后我搜索了头后,当我点击一个特定的头它必须只显示h_id不是所有的细节,然后我保存的新成员按照该H_id细节。请帮助。

My problem is that I want to first search heads details, then after i searched the heads details when i click a particular head it must only display the h_id not all details then i save a new member under that H_id. Please help.

推荐答案

让我们开始,我们将调用首页的初始视图。因此,我们需要一个视图模型它,让我们把它叫做 HeadVM

Lets start with an initial view we will call Index. So we will need a ViewModel for it, lets call it HeadVM

[Serializable]
public class HeadVM
{
    public List<Heads> data { get; set; }      
}
public class Heads
{
    public int h_id { get; set; }
    public string fname { get; set; }
    public string lname { get; set; }
}

我假设,我们只是被填充的所有头的完整列表,整个初始视图。

I am assuming we will just be populating the entire initial view with a complete list of all the heads.

现在我们的行动:

public ActionResult Index()
{
    HeadVM list = new HeadVM()
        {
            data = new List<Heads>()
        };
        var AllHeads = context.Heads;
        foreach (var item in AllHeads)
        {
            if (item != null)
            {
                list.data.Add(new Heads
                {
                    h_id = item.h_id,
                    fname = item.fname,
                    lname = item.lname,
                });
            }
        }
        return View(list);
}

这将返回的列表,你的首页视图,在这里你可以简单地显示和访问每个的一个按钮。在这种情况下,我用一个表,我发现这是最简单的显示一个列表:

This will return a list of Heads to your Index view, where you can simply show and access each Head with a button. In this case I use a table as I find it is the simplest to display a list:

@model ...HeadVM
<table border="1">
<tr>
    <th>HeaderID</th>
    <th>First Name</th>
    <th>Last Name</th>
    <td></td>
</tr>
@foreach(var item in Model.data)
<tr>
    <td>@item.h_id</td>
    <td>@item.fname</td>
    <td>@item.lname</td>
    <td>@Html.ActionLink("Add Member", "Create", "Home", new { id = item.h_id }, new { target = "_blank" })</td>
</tr>

这会给首长名单,每行的添加成员在一个按钮(因为我不知道你们的关系究竟是如何工作的,我将承担成员是1:许多关系,纯粹为了一个功能更强大的系统同时,由于新{目标=_blank}的,创建视图将在新的窗口中启动。

This will give a list of heads, with a button in each row "Add member"(As I don't know exactly how your relationships work, I am going to assume Head to Member is in 1:Many relationship. Purely for a more functional system. Also, because of new { target = "_blank"}, the Create view will start in new window.

然后,我们开始我们的 [HTTPGET] 动作:

Then we start on our [HttpGet] action:

public ActionResult Create(int id)
    {
        var temp = context.Heads.Find(id);
        MembersViewModel tmp = new MembersViewModel
        {
            h_id = temp.h_id,
        };
        return View(tmp);
    }

这将现在ppopulated h_id 字段$ P $返回了一个模型来你的创建视图。不应该有从这里就增加一个新成员与ppopulated h_id ,我们曾通过在previous答案<一的$ P $的麻烦href=\"http://stackoverflow.com/questions/26669740/i-want-to-save-an-id-from-a-table-to-another-table-using-asp-net-mvc-5/26671649?noredirect=1#comment42013989_26671649/\">Saving从另一个表 ID

This will now return the a model to your 'Create' view with prepopulated h_id field. There shouldn't be much trouble from here regarding adding a new member with the prepopulated h_id that we worked through in your previous answer Saving ID from another table

这更是一个比指导的精确复制粘贴的答案,因为我不完全理解你的系统。另外,我看到标题实际上来自另一个表,因此该值不能直接从头表中,除一个 INT ID 。这是我的理由不引用标题字段。

This is more of a guideline than an exact copy paste answer, as I don't fully understand your system. Also, I see Title actually comes from another table, so the value isn't available directly from the 'Head' table, except an int value ID. This was my reason for not referencing the Title field.

希望这可以帮助您与您的项目

Hope this helps you with your project

这篇关于如何寻找一个头,然后保存一个新成员的头部asp.net的MVC 5下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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