简体   繁体   English

自动调用控制器方法并生成局部视图。怎么样?

[英]Automatically call controller method and generate partial view. How?

I want to automatically generate a list in a partial view. 我想在局部视图中自动生成一个列表。

_Layout.cshtml _Layout.cshtml

@Html.Partial("../Transaction/_Transaction")

TransactionController TransactionController

public JsonResult transactionlist()
        {
            List<Transaction> TransactionList = new List<Transaction>().ToList();
            string Usercache = MemoryCache.Default[User.Identity.Name] as string;

            int UsercacheID = Convert.ToInt32(Usercache);
            if (Usercache == null)
            {
                int UserID = (from a in db.UserProfiles
                              where a.UserName == User.Identity.Name
                              select a.UserId).First();
                UsercacheID = UserID;
                MemoryCache.Default[User.Identity.Name] = UsercacheID.ToString();
            }
            var Account = (from a in db.UserAccount
                           where a.UserId == UsercacheID
                           select a).First();

            var DBTransaction = from a in db.Transaction
                                where a.AccountId == Account.AccountId
                                select a;

            var DBTransactionList = DBTransaction.ToList();
            for (int i = 0; i < DBTransactionList.Count; i++)
            {
                TransactionList.Add(DBTransactionList[i]);
            }
            ViewBag.acountsaldo = Account.Amount;

            return Json(TransactionList, JsonRequestBehavior.AllowGet);
        }`

How should my _Transaction.cshtml be coded to make a simple list without submit buttons etc.? 如何_Transaction.cshtml来制作一个没有提交按钮等的简单列表?

You should call your controller action and have it return your partial view. 您应该调用控制器操作并让它返回您的局部视图。 Also, use a view model instead of viewbag. 此外,使用视图模型而不是viewbag。

Layout or parent view/partial view: 布局或父视图/局部视图:

@Html.Action("Transaction", "YourController")

Partial view: 局部视图:

@model TransactionModel
@foreach (Transaction transaction in Model.TransactionList) {
    // Do something with your transaction here - print the name of it or whatever
}

View Model: 查看型号:

public class TransactionModel {
    public IEnumerable<Transaction> TransactionList { get; set; }
}

Controller: 控制器:

public class YourController
{
    public ActionResult Transaction()
        {
            List<Transaction> transactionList = new List<Transaction>().ToList();
            // Your logic here to populate transaction list
            TransactionModel model = new TransactionModel();
            model.TransactionList = transactionList;

            return PartialView("_Transaction", model);
        }
}

One way is to have a page return a list of items and feed them into your partial. 一种方法是让页面返回一个项目列表并将它们提供给您的部分。

function ActionResult GetMainTransactionView()
{
  List<Transaction> transactions=GetTransactions();
  return PartialView("TransactionIndex",transactions);
}

TransactionIndex.cshtml TransactionIndex.cshtml

@model List<Transaction>
@Html.Partial("../Transaction/_Transaction",model)

Main.chtml Main.chtml

<a id="transactionLink" href='@Url.Action("GetMainTransactionView","Transaction")'/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM