如何使用TPH方法为多个模型创建单个创建操作

时间:2016-04-06 10:05:18

标签: asp.net-mvc view binding

我有基类合同

struct message{
    int id;
    char *msg;
    const string time;
};

另外两个类来自这个类:CreditContract和PrepaymentContract。

public abstract class Contract
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ContractID { get; set; }

    [DataType(DataType.Date)]
    [Required]
    public DateTime StartDate { get; set; }

    [DataType(DataType.Date)]
    [Required]
    public DateTime EndDate { get; set; }
}

此外,我已经创建了ContractViewModel类。

public class CreditContract : Contract
{
    [Required]
    public float CreditLimit { get; set; }

    [Column(TypeName = "char")]
    [StringLength(3)]
    [Required]
    public string CreditLimitCurrency { get; set; }
}

public class PrepaymentContract : Contract
{
    [Required]
    public float PrepaymentAmount { get; set; }

    [Column(TypeName = "char")]
    [StringLength(1)]
    [Required]
    public string PrepaymentPeriod { get; set; }
}

我的Create方法如下所示:

public int SelectedContract { get; set; }
public CreditContract creditContract { get; set; }
public PrepaymentContract prepaymentContract { get; set; }

我的创建视图如下所示:

public ActionResult Create(ContractViewModel contract)
    {
        if (contract.SelectedCategory == 1)
        {
            if (ModelState.IsValid)
            {
                contract.creditContract.StartDate = contract.StartDate;
                contract.creditContract.EndDate = contract.EndDate;
                db.Contracts.Add(contract.creditContract);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(contract.creditContract);
        }
        else
        {
            if (ModelState.IsValid)
            {
                contract.prepaymentContract.StartDate = contract.StartDate;
                contract.prepaymentContract.EndDate = contract.EndDate;
                db.Contracts.Add(contract.prepaymentContract);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(contract.prepaymentContract);
        }
    }

我可以这样做:

E.g:

@model CodeFirstInheritance.ViewModels.ContractViewModel
    <div class="form-horizontal">
    <h4>Course</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <div class="form-group">
        @Html.LabelFor(model => model.creditContract.StartDate, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.creditContract.StartDate, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.creditContract.StartDate, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.creditContract.EndDate, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.creditContract.EndDate, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.creditContract.EndDate, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.SelectedCategory, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.SelectedCategory, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.SelectedCategory, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.creditContract.CreditLimit, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.creditContract.CreditLimit, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.creditContract.CreditLimit, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.creditContract.CreditLimitCurrency, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.creditContract.CreditLimitCurrency, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.creditContract.CreditLimitCurrency, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>

返回视图(selectedContract);

0 个答案:

没有答案