0

我是 asp .net mvc 4.0 的新手。我给了模型。我不知道如何为模型创建视图。我在IList JournalEntries 遇到问题。我能做的其他条目。

public class Journal : BaseClass
{
    public virtual string VoucherNo { get; set; }
    public virtual DateTime VoucherDate { get; set; }
    public string VoucherDateView {
        get
        {
            return VoucherDate.ToShortDateString();
        }
    }
    public IList<JournalEntry> JournalEntries { get; set; }
    public IList<Ledger> Accounts { get; set; }
    public double TotalAmount
    { 
        get 
        { 
            double sum = 0;
            if (JournalEntries != null && JournalEntries.Count>0)
            foreach (var journal in JournalEntries)
                sum = journal.Principal + journal.Interest+sum;
            return sum;
        } 
    }
}

我试过下面的视图,但添加条目不起作用。

    @model Sms.CoreSociety.Journal

    @{
    ViewBag.Title = "Create";
    }
    @{
    string data = new     System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model);
    }

    <script type="text/javascript">
    $(document).ready(function () {
        $('#document').validate();
        $("#VoucherDate").mask("99/99/9999", { placeholder: " " });
        function entryVm(entries) {
                var self = this;

                self.entryList = ko.observableArray(entries);
                self.entry = ko.observable();
                self.rowClick = function(entry1) {
                                                    alert("Delete alert");
                                                    self.dispatchList.remove(entry1);
                                                };
                self.addEntry = function() {
                                            alert("Add alert");
                                            this.entryList.push({ AccountName_AccountHead:     "", DebitCredit: "", Principal: "0.0", Interest: "0.0", Narration: ""});
                                            };

            }

            var models = @Html.Raw(Json.Encode(Model.JournalEntries)) ;
            ko.applyBindings(new entryVm(models));

    });
    </script>
    @using (Html.BeginForm(null, null, FormMethod.Post, new Dictionary<string, object>() {     { "class", "form-horizontal" }, { "id", "document" } }))
    {
        @Html.ValidationSummary(true)

        <fieldset>
            <div class="row">
                <div class="span1">
                    <label>Voucher No</label>
                </div>
                <div class="span5">
                    @Html.DisplayFor(model => model.VoucherNo)
                </div>
            </div>
            <div class="row">
                <div class="span1">
                    <label>Voucher Date</label>
                </div>
                <div class="span5">
                   @Html.TextBoxFor(model => model.VoucherDate, "{0:dd/MM/yyyy}", new Dictionary<string, object>() { { "class", "required" } })
                </div>
            </div>
            <div class="row">
                <div class="span1">
                    <label>Amount</label>
                </div>
                <div class="span5">
                    @Html.DisplayFor(model => model.TotalAmount)
                </div>
            </div>
            <input type="submit" value="Save" class="btn" id="submit"/>
            @if (Model.Id != new Guid())
            {
                <div style="float: right">
                    <a class="btn btn-danger" href='@Url.Action("Delete")/@Model.Id' aria-hidden="true">Delete</a>
                </div>
            }        
        </fieldset>
}
    <h4>Journal Entry</h4>
    <p >Entry for<span data-bind="text: entryList().length">&nbsp;</span> entry(s)</p>
    <button data-bind="click: addEntry" class="btn">Add Record</button>
    <table>
    <tbody data-bind="template: { name: 'entryRowTemplate', foreach: entryList }"></tbody>
    </table>

    <script type="text/html" id="entryRowTemplate">
    <tr>
        <td>AccountName_AccountHead: \$ <input data-bind="value: AccountName.AccountHead"/>        </td>
        <td>DebitCredit: \$ <input data-bind="value: DebitCredit"/></td>
        <td>Principal: \$ <input data-bind="value: Principal"/></td>
        <td>Interest: \$ <input data-bind="value: Interest"/></td>
        <td>Narration: \$ <input data-bind="value: Narration"/></td>
        <td><a href="#" data-bind="click: $parent.rowClick">Delete</a></td>
    </tr>
    </script> 

下面是我的日记控制器

using System;
using System.Linq;
using System.Web.Mvc;
using Sms.CoreSociety;
using System.Collections.Generic;

namespace SmsModernUI.Controllers
{
    public class JournalController : BaseController
    {
        //
        // GET: /AccountGroup/

        public ActionResult Index()
        {
            var journals = Repository.GetAll<Journal>().OrderBy(x => x.VoucherNo);
            return View(journals);
        }
        public ActionResult Create(Guid id)
        {
            if (id == new Guid())
            {
                var journal = new Journal();
                string lastvoucherno = Repository.GetAll<Journal>().OrderBy(x => x.VoucherNo).Last().VoucherNo;
                journal.VoucherNo = (int.Parse(lastvoucherno) + 1).ToString();
                journal.VoucherDate = System.DateTime.Now;
                journal.JournalEntries = new List<JournalEntry>();
                journal.Accounts = Repository.GetAll<Ledger>();
                return PartialView(journal);
            }
            var journal1 = Repository.Get<Journal>(id);
            journal1.JournalEntries = Repository.GetAll<JournalEntry>(x => x.Journal.Id == id);
            journal1.Accounts = Repository.GetAll<Ledger>();
            return PartialView(journal1);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(Journal journal)
        {
            if (journal.Id == new Guid())
            {
                var jj = Repository.Save(journal);
                foreach (var journalentry in journal.JournalEntries)
                {
                    journalentry.Id = jj.Id;
                    Repository.Save(journalentry); 
                }
            }
            else
            {
                Journal jr = Repository.Get<Journal>(journal.Id);
                var entries = Repository.GetAll<JournalEntry>(x=>x.Journal.Id == journal.Id);
                foreach (var entry in entries)
                {
                    Repository.Delete(entry);
                }
                var jj = Repository.Save(journal);
                foreach (var journalentry in journal.JournalEntries)
                {
                    journalentry.Id = jj.Id;
                    Repository.Save(journalentry);
                }
            }
            return RedirectToAction("Index");
        }
        public ActionResult Index1()
        {
            Journal journal1 = Repository.Get<Journal>(new Guid("7A6EEBBC-2F3A-4A27-ACF8-A1D40115A68F"));
            journal1.JournalEntries = Repository.GetAll<JournalEntry>(x => x.Journal.Id == journal1.Id);
            journal1.Accounts = Repository.GetAll<Ledger>();
            return View(journal1);
        }
        public ActionResult Delete(Guid id)
        {
            Journal jr = Repository.Get<Journal>(id);
            var entries = Repository.GetAll<JournalEntry>(x => x.Journal.Id == jr.Id);
            foreach (var entry in entries)
            {
                Repository.Delete(entry);
            }

            var result = Repository.Delete(jr);

            return RedirectToAction("Index");
        }
        [HttpPost]
        public ActionResult Create1(Journal journal)
        {
            var temp = journal;
            return RedirectToAction("Create",journal.Id);
        }

    }
}
4

3 回答 3

1

视图不是从模型中生成的。您需要 Controller Action 方法将模型传递给 View。

public ActionResult()
{
 var model = new Journal
  {
   //**define here value of model's properties, that you need in View
   }

return View(model);
}

编辑:添加后。我会把它分成两部分。创建 ViewModel 并将其从 View 传递给 Controller。

public JurnalViewModel
{
public Journal journal {get; set;}
public IList<JournalEntry> JournalEntries {get; set;}
}

比在模型中的Create action第一次创建journal和 foreach 之后JournalEntries创建新的JournalEntry

已编辑 2对您的评论。快速示例:

 [HttpPost]
 public ActionResult Create (JurnalViewModel model)
{
 var journal = new Journal();
 db.Journals.Add(journal);
 journal.name = model.journal.name
 .....
//**some code
db.SaveChanges()

  foreach(var item in model.JournalEntries )
{
  var entry = new JournalEntry()
  db.JournalEntries .Add(entry);
  entry.property = item.property;
  ....
  //**some code
  db.SaveChanges()
 }
}
于 2013-09-10T15:14:42.997 回答
0

右键单击控制器内的 Action 方法,然后单击添加视图,然后选中创建强类型视图复选框,然后从显示的对话框中的下拉列表中选择所需的模型

于 2013-09-10T15:41:40.113 回答
0

您的问题是您没有 JournalEntries 的类构造函数。

public Journal()
{
    JournalEntries = new List<JournalEntry>();
    Accounts = new List<Ledger>();
}
于 2013-09-10T15:16:01.393 回答