powered by simpleCommunicator - 2.0.59     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / ASP.NET [игнор отключен] [закрыт для гостей] / 2 модели в 1 view
1 сообщений из 1, страница 1 из 1
2 модели в 1 view
    #38574215
kim-kong
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Использую 2 модели в 1 вью, одна для вывода грида, вторая для форм, которые заполняют этот самый грид. Как мне написать такой [HttpPost], чтобы вносить в БД только IngList из IngListView ?

Код: c#
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
    public class IngListView

    public class IngListModel
    {
        public int ArticleId { get; set; }
        public int IngListId { get; set; }
        public int ProdId { get; set; }
        public int IngId { get; set; }
        public string ProdName { get; set; }
        public string IngName { get; set; }
        public float Amount { get; set; }
        public float? Percent { get; set; }
        public float? losses { get; set; }
        public float? total_losses { get; set; }
        public float? reporting_losses { get; set; }
        public float? total_reporting_losses { get; set; }
        public float? sum { get; set; }
    }

    public partial class IngList
    {
        public int Id { get; set; }
        public int GoodId { get; set; }
        public int ArticleId { get; set; }
        public float amount { get; set; }
        public float losses { get; set; }
        public float reporting_losses { get; set; }
    
        public virtual Article Article { get; set; }
        public virtual Good Good { get; set; }
    }

    public class IngListView
    {
        public IngList IngList { get; set; }
        public IEnumerable<EnzoApplication.Models.IngListModel> IngListModel { get; set; }
    }



Код: c#
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
        public ActionResult IngList1(int id)
        { 
            ViewBag.Ingredient = new SelectList(db.Goods, "Id", "Name");
            ViewBag.Art = new SelectList(db.Articles, "Id", "ArticleName", ViewBag.Ar);
            ViewBag.Name = from a in db.Articles where a.Id == id select a.ArticleName;
            ViewBag.ProductName = from a in db.Articles
                                  join p in db.Products on a.ProductId equals p.Id
                                  where (a.Id == id) && (p.Id == a.ProductId) 
                                  select p.Name;
            ViewBag.Ar = id;
            ViewBag.Sum = db.IngLists.Where(x => x.ArticleId == id).Sum(x => (float?)x.amount);
            var sum = db.IngLists.Where(x => x.ArticleId == id).Sum(x => (float?)x.amount);

            var viewmodel = new IngListView
                {
                    IngListModel =
                    (from il in db.IngLists
                     join a in db.Articles on il.ArticleId equals a.Id
                     join p in db.Products on a.ProductId equals p.Id
                     join g in db.Goods on il.GoodId equals g.Id
                     where (a.Id == id) && (il.GoodId == g.Id)
                     select new IngListModel { ArticleId = a.Id, IngListId = il.Id, ProdId = id, ProdName = p.Name, IngName = g.Name, Amount = il.amount, Percent = il.amount * 100 / sum, losses = il.losses, reporting_losses = il.reporting_losses, total_losses = il.losses * il.amount / 100, total_reporting_losses = il.reporting_losses * il.amount / 100, sum = sum }).ToList(),
                    IngList = new IngList(),
                };
            return View(viewmodel);
        }

        [HttpPost]
        public ActionResult IngList1(IngListView inglistview)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.[color=red]IngLists[/color].Add(inglistview);
                    db.SaveChanges();
                    return RedirectToAction("IngList1");
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(String.Empty, ex);
            }
            return View();
        }



Код: html
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
@model EnzoApplication.Models.IngListView


@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>IngList1</legend>
<table class ="no_border">
    <tr>
    <td>
        <div class="editor-field">

            @Html.HiddenFor(model => model.IngList.ArticleId, new { @Value = ViewBag.Ar })
            Сырье
        </div>

        <div class="editor-field">
            @Html.DropDownList("GoodId", (IEnumerable<SelectListItem>)ViewBag.Ingredient, "--Выберите сырье--", new { @style = "width: 310px;" })
            @Html.ValidationMessageFor(model => model.IngList.GoodId)
        </div>
        </td>
        <td>
        <div class="editor-label">
            Количтво
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.IngList.amount, new { @style = "width: 260px;" })
        </div>
        </td>
        </tr>
            <tr>
        <td>
        <div class="editor-label">
            Фактические потери
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.IngList.losses, new { @style = "width: 160px;" })
        </div>
        </td>
        <td>
        <div class="editor-label">
            Отчетные потери
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.IngList.reporting_losses, new { @style = "width: 160px;" })
        </div>
        </td>
                <td>
        <p>
            <input type="submit" value="Добавить" />
        </p>
                    </td>
        </tr>
        </table>
    </fieldset>
}

@{
    var grid = new WebGrid(Model.IngListModel,
        defaultSort: "ProdName",
        rowsPerPage: 3000,
        columnNames: new[] { "IngName", "Amount", "Percent", "losses", "reporting_losses", "total_losses", "total_reporting_losses" }
        );
}


@grid.Table(
            columns: grid.Columns(
            grid.Column("IngName", header: "Сырье"),
            grid.Column("Amount", header: "Кг"),
            grid.Column("Percent", header: "%", format: @<text> @item.Percent.ToString("0.00") %</text>),
            grid.Column("losses", header: "% потерь", format: @<text> @item.losses.ToString("0.00") %</text>),
            grid.Column("total_losses", header: "Фактические потери", format: @<text> @item.total_losses.ToString("0.00")</text>),
            grid.Column("reporting_losses", header: "% отчетных потерь", format: @<text> @item.reporting_losses.ToString("0.00") %</text>),
            grid.Column("total_reporting_losses", header: "Отчетные потери", format: @<text> @item.total_reporting_losses.ToString("0.00")</text>),
            grid.Column("", header: "", format: @<text>@Html.ActionLink("Удалить", "DeleteIngList", new { id = item.IngListId, ArticleId = item.ArticleId }, new { @class = "btn btn-success" })</text>)
            )
            //footer: @<text>Итог:  @ViewBag.sum.ToString("0.00") кг, Фактические потери: @ViewBag.los.ToString("0.00") кг, Отчетные потери: @ViewBag.replos.ToString("0.00") кг</text>
            )
...
Рейтинг: 0 / 0
1 сообщений из 1, страница 1 из 1
Форумы / ASP.NET [игнор отключен] [закрыт для гостей] / 2 модели в 1 view
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]