Viewmodel显示一切

时间:2015-04-24 10:44:42

标签: c# asp.net-mvc

尝试学习和理解ViewModels作为我遇到的问题的解决方案。到目前为止,我有这个:

    public class PrinterCreditsModel
{
    public string Username { get; set; }
    public int AmountAdded { get; set; }
    public string AddedBy { get; set; }
    public DateTime AddedWhen { get; set; }
    public string Money { get; set; }
    public tblOption PrinterCreditFund { get; set; }
}

它们都来自"tblPrinterCredits""PrinterCreditFund"来自"tblOption"

我已经在创建页面上更改了模型以使用viewmodel,并为PrinterCreditFund属性添加了@ html.labelfor等,但它只是显示该数据库表中的所有内容。任何有关为什么或正确方向的观点的帮助都会受到赞赏:)

由于

这是视图的代码 -

@model Dashboard.ViewModels.PrinterCreditsModel

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

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

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

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

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

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

控制器:

public ActionResult Create()
    {
        ViewBag.OptionID = new SelectList(db.tblOptions, "ID", "PrinterCreditFund");
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "PrinterCreditID,Username,AmountAdded,AddedBy,AddedWhen,Money,OptionID")] tblPrinterCredit tblPrinterCredit)
    {
        if (ModelState.IsValid)
        {              
            db.tblPrinterCredits.Add(tblPrinterCredit);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.OptionID = new SelectList(db.tblOptions, "ID", "PrinterCreditFund", tblPrinterCredit.OptionID);
        return View(tblPrinterCredit);
    }

这是tblOptions模型,用于显示表格中的内容:

public int ID { get; set; }
public int Visible { get; set; }
public int DeleteLicense { get; set; }
public Nullable<int> ICTSurvey { get; set; }
public Nullable<int> PaidWorkSurvey { get; set; }
public Nullable<int> ICTGeneralSurvey { get; set; }
public Nullable<int> PESSCLSurvey { get; set; }
public Nullable<int> PastSurvey { get; set; }
public Nullable<int> PresentSurvey { get; set; }
public Nullable<int> Yr11Survey { get; set; }
public Nullable<int> NewScientistCount { get; set; }
public Nullable<int> UCASYear { get; set; }
public Nullable<int> OnlineSurveyActive { get; set; }
public Nullable<int> OnlineExaminationsActive { get; set; }
public string UCASFirstHalf { get; set; }
public string UCASSecondHalf { get; set; }
public string SIMSManual { get; set; }
public string SIMSApplication { get; set; }
public string SIMSAmpark { get; set; }
public string PrinterCreditFund { get; set; }
public string PrinterCreditCost { get; set; }
public string UCASIndividualFirstHalf { get; set; }
public string UCASIndividualSecondHalf { get; set; }
public string BookingSheetYear { get; set; }
public Nullable<System.DateTime> InductionDate { get; set; }
public string InductionYear { get; set; }

问题详情页面:

    @model Dashboard.tblPrinterCredit

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

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

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

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

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

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

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

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

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

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

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

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

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

    </dl>
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.PrinterCreditID }) |
    @Html.ActionLink("Back to List", "Index")
</p>

详情动作结果:

    public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        tblPrinterCredit tblPrinterCredit = db.tblPrinterCredits.Find(id);
        if (tblPrinterCredit == null)
        {
            return HttpNotFound();
        }
        return View(tblPrinterCredit);
    }

Create Page

上图显示了它。标记为蓝色的是tblPrinterCredits所显示的内容,它应该都在那里。红色来自tblOption,其中只有PrinterCreditFund应显示

0 个答案:

没有答案