使用其他模型

时间:2018-03-15 21:35:45

标签: c# asp.net-core

我想创建仅用于创建和编辑我的TicketController的下拉列表。

public class Ticket
{
    private DateTime _createDate;
    private DateTime _updateDate;
    private Priority _priority;
    private int _id;

    [Key]
    [Display(Name = "ID")]
    public int TicketId { get => _id; set => _id = value; }
    [Display(Name = "Created by")]
    public string OwnerId { get; set; }
    public User Owner { get; set; }
    [Display(Name = "Assigned to")]
    [DisplayFormat(NullDisplayText = "No one is assigned to this ticket")]
    public string EmployeeId { get; set; }
    public User Employee { get; set; }
    [Display(Name = "Product")]
    public int? ProductId { get; set; }
    public Product Product { get; set; }

    [Required]
    [StringLength(60, MinimumLength = 6)]
    public string Title { get; set; }

    [Required]
    [StringLength(60, MinimumLength = 6)]

    public string Description { get; set; }

    public Status Status { get; set; }

    public Priority Priority { get => _priority; set => _priority = value; }

    [Display(Name = "Create Date")]
    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:f}")]
    public DateTime CreateDate { get => _createDate; set => _createDate = DateTime.Now; }

    [Display(Name = "Update Date")]
    [DisplayFormat(ApplyFormatInEditMode =true,DataFormatString = "{0:f}")]
    [DataType(DataType.Date)]
    public DateTime UpdateDate { get => _updateDate; set => _updateDate = DateTime.Now; }

    [Display(Name = "Comments")]
    [DisplayFormat(NullDisplayText = "No comments for this Ticket")]
    public ICollection<Comment> Comments { get; set; }

    public bool Assigned { get; set; }
}
public enum Status
{
    Queue,
    Progress,
    Completed
}
public enum Priority
{
    Low,
    Medium,
    High
}

我想在DropdownList的Create模式下显示这个模型:

public class Product
{
    [Key]
    public int ProductId { get; set; }
    public string Name { get; set; }
}

我不知道,如何应对它。

1 个答案:

答案 0 :(得分:0)

您最好的方法是将数据库模型与View模型分开。将一个模型用于一切并不是一个好主意。您可以创建新的TicketModel来保存您的Ticket和所有可用产品。这是一个小例子,你如何实现这个目标:

简化型号:

using Microsoft.AspNetCore.Mvc.Rendering;
using System.Collections.Generic;

namespace Testing
{
    public class Ticket
    {
        public int TicketId { get; set; }
        public Product Product { get; set; }
    }

    public class Product
    {
        public int ProductId { get; set; }
        public string Name { get; set; }
    }

    public class TicketModel
    {
        public string ProductId { get; set; }
        public Ticket Ticket { get; set; }
        public IEnumerable<SelectListItem> AvailableProducts { get; set; }
    }
}

控制器:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Linq;

namespace Testing
{
    public class TestController : Controller
    {
        public IActionResult Index()
        {
            // Get Products from database
            Product[] products = new Product[]
            {
                new Product { ProductId = 1, Name = "Item 1" },
                new Product { ProductId = 2, Name = "Item 2" },
                new Product { ProductId = 3, Name = "Item 3" }
            };

            // Create your TicketModel from Ticket and Products in here
            TicketModel model = new TicketModel
            {
                Ticket = new Ticket() { TicketId = 1 },
                AvailableProducts = products.Select(x => new SelectListItem { Value = x.ProductId.ToString(), Text = x.Name })
            };

            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Index(TicketModel model)
        {
            if (ModelState.IsValid)
            {
                // Create your Ticket from TicketModel in here
            }
            else
            {
                // In case of errors recreate AvailableProducts
            }

            return View(model);
        }
    }
}

查看:

@model Testing.TicketModel

<div class="row">
    <div class="col-md-4">
        <form asp-action="Index">
            <div class="form-group">
                <div class="checkbox">
                    <label asp-for="Ticket.TicketId">Ticket Id: </label>
                    <input asp-for="Ticket.TicketId" />
                </div>
            </div>
            <div class="form-group">
                <select asp-for="ProductId" asp-items="Model.AvailableProducts"></select>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>