单一视图页面中的多表Asp.net 4

问题描述 投票:0回答:1

我试图在一个View中显示两个2表。我不想以列表的形式显示,而是以细节的形式显示,大多数教程我得到的是一个列表视图。

我尝试了下面的视频。https:/youtu.beoN1f2Vpc-wU

另一个链接。https:/docs.microsoft.comen-usaspnetmvcoverviewolder-versionsgetting-started-with-ef-5-using-mvc-4reading-related-data-with-entity-framework-in-an-asp-net-mvc-application。

现在我的问题是,当我点击链接打开ViewModel的详情页时,它显示为null。

模型1

[Table("UserRegistration")]
public partial class UserRegistration
{
    [Key]
    public Guid ClientId { get; set; }

    [StringLength(50)]
    public string FirstName { get; set; }

    [StringLength(50)]
    public string LastName { get; set; }

    [StringLength(50)]
    public string IdentityNumber { get; set; }

    [StringLength(50)]
    public string PhoneNumber { get; set; }

    public string Password { get; set; }

    public string ConfirmPassword { get; set; }

    [Column(TypeName = "date")]
    public DateTime? DateCreated { get; set; }
}

型号2

[Table("Voucher")]
public partial class Voucher
{
    public int VoucherId { get; set; }

    public Guid? CustomerId { get; set; }

    [StringLength(50)]
    public string HouseNumber { get; set; }

    [StringLength(50)]
    public string PostalCode { get; set; }

    [StringLength(50)]
    public string Location { get; set; }

    [Column(TypeName = "date")]
    public DateTime? DateCreated { get; set; }
}

查看模型

 public class ViewModel
{

    public Guid? CustomerId { get; set; }

    public string FirstName { get; set; }


    public string LastName { get; set; }


    public string IdentityNumber { get; set; }


    public string HouseNumber { get; set; }


    public string PostalCode { get; set; }


    public string Location { get; set; }
} 

控制器用户注册

  private mymodel db = new mymodel();

    // GET: UserRegistrations
    public ActionResult Index()
    {
        return View(db.UserRegistrations.ToList());
    }

控制器首页

    private mymodel db = new mymodel();
    // GET: Home
    public ActionResult Index(Guid id)
    {
        //
        UserRegistration ur = db.UserRegistrations.Single(x => x.ClientId == id);
        Voucher voucher = new Voucher();

        ViewModel viewm = new ViewModel();

        ur.FirstName = viewm.FirstName;
        ur.LastName = viewm.LastName;

        voucher.HouseNumber = viewm.HouseNumber;
        voucher.Location = viewm.Location;
        voucher.PostalCode = viewm.PostalCode;


        return View(viewm);
    }

Index.cshtml

          @model ExampleTestMVC.Models.ViewModel
    @{
        ViewBag.Title = "Index";
     }

     <h2>Index</h2>


     <div>
     <h4>UserRegistration</h4>
     <hr />
     <dl class="dl-horizontal">
     <dt>
       @Html.DisplayNameFor(model => model.FirstName)
      </dt>

      <dd>
        @Html.DisplayFor(model => model.FirstName)
      </dd>

      <dt>
        @Html.DisplayNameFor(model => model.LastName)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.LastName)
    </dd>


    <dt>
        @Html.DisplayNameFor(model => model.HouseNumber)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Location)
    </dd>


    <dt>
        @Html.DisplayNameFor(model => model.PostalCode)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.PostalCode)
    </dd>

</dl>

用户注册的index.chtml

   @model IEnumerable<ExampleTestMVC.Models.UserRegistration>

   @{
    ViewBag.Title = "Index";
    }

   <h2>Index</h2>

   <p>
    @Html.ActionLink("Create New", "Create")
   </p>
   <table class="table">
   <tr>
    <th>
        @Html.DisplayNameFor(model => model.FirstName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.LastName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.IdentityNumber)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.PhoneNumber)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Password)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ConfirmPassword)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.DateCreated)
    </th>
    <th></th>
</tr>

  @foreach (var item in Model) {
    <tr>
    <td>
        @Html.DisplayFor(modelItem => item.FirstName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.LastName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.IdentityNumber)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.PhoneNumber)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Password)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ConfirmPassword)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.DateCreated)
    </td>
    <td>
        @Html.ActionLink("details of everything", "Index", "Home", new { id = item.ClientId }, null) |     
       </td>
       </tr>
       }

      </table>
asp.net asp.net-mvc asp.net-mvc-4 asp.net-mvc-3 ef-code-first
1个回答
1
投票

你没有设置值的viewmodel是绑定的详细视图。改变主控制器的代码,像这样。

public ActionResult Index(Guid id)
{
    //
    UserRegistration ur = db.UserRegistrations.Single(x => x.ClientId == id);
    Voucher voucher = db.Vouchers.Single(x => x.CustomerId == id);

    ViewModel viewm = new ViewModel();

    viewm.FirstName=ur.FirstName;
    viewm.LastName=ur.LastName;

    viewm.HouseNumber=voucher.HouseNumber;
    viewm.Location=voucher.Location;
    viewm.PostalCode=voucher.PostalCode;


    return View(viewm);
}
© www.soinside.com 2019 - 2024. All rights reserved.