【问题标题】:asp.net | Data transfer from the Db by the Controller to the ViewASP.NET |控制器从 Db 到 View 的数据传输
【发布时间】:2020-02-08 12:35:36
【问题描述】:

描述:

  • 我使用 EF 从数据库中获取信息
var card= _ecpContext.Card.Where(sth).ToList();
  • 这给了我列表格式的信息

    卡数 = 31

    [0] {App.Models.Card}

    [1] {App.Models.Card}

    [2] {App.Models.Card}

  • 我想将此数据从控制器传输到视图

  • 我正在尝试这样做:

控制器:

[HttpPost]
public async Task<ActionResult> MethodsReturnView(string previously_uploaded_data_from_ajax)
{

var userName = _httpContextAccessor.HttpContext.User.Identity.Name;
            var userDetailsAU = _ecpContext.AC_Merge_V.FirstOrDefault(f => f.ADlogin == userName);
            var userDetails = _context.Uzytkownicy.FirstOrDefault(f => f.Adlogin == userName);

            dynamic serializer = JsonConvert.DeserializeObject<IDictionary>(previously_uploaded_data_from_ajax);

            var numerMiesiacaObject = serializer["numerMiesiaca"];
            var numerRokuObject = serializer["numerRoku"];
            var liczbaDniObject = serializer["liczbaDni"];


            int numerMiesiaca = Convert.ToInt32(numerMiesiacaObject);
            int numerRoku = Convert.ToInt32(numerRokuObject);
            int liczbaDni = Convert.ToInt32(liczbaDniObject);


            ViewBag.numerMiesiaca = numerMiesiaca;
            ViewBag.numerRoku = numerRoku;
            ViewBag.liczbadni = liczbaDni;


      var dbExists = _ecpContext.Karta.FirstOrDefault(f => f.DzMiesiaca == 1 && f.Miesiac == numerMiesiaca && f.Rok == numerRoku && f.Login == userName);
            if (dbExists == null)
            {
                List<Karta_Model> objKartaModel = new List<Karta_Model>();

                for (int i = 1; i <= liczbaDni; i++)
                {

                    var model = new Karta_Model()
                    {
                      // sth
                    };

                    objKartaModel.Add(model);

                }



                await _ecpContext.Karta.AddRangeAsync(objKartaModel);
                await _ecpContext.SaveChangesAsync();
            }
            var karta = _ecpContext.Karta.Where(f => f.Miesiac == numerMiesiaca && f.Rok == numerRoku && f.Login == userName).OrderBy(x => x.Rok).ThenBy(x => x.Miesiac).ThenBy(x => x.DzMiesiaca).ToList();


     return PartialView("_ReturnView", karta);
}

问题:

  • 但如果它在返回的视图附近添加“卡片”会引发错误

    加载资源失败:服务器响应状态为 500(内部服务器错误)

  • 调用返回命令时抛出错误(已调查,红点)

  • 不发送此数据,视图正常返回

另外:

我的模特:

public partial class Karta_Model
{
    [Key]
    public int Id { get; set; }
    public int? NrDay{ get; set; }
    public int? NrMonth { get; set; }
    public int? NrYear{ get; set; }
    public string? Rozpoczecie { get; set; } 
    public string? Zakonczenie{ get; set; } 
    public string? OdbiorGodzin{ get; set; } 
    ...
}

public partial class ParentView
{
    public List<Karta_Model> Model1 { get; set; }
}

查看:

using AppEcp.Models
@model ParentView

 @for (int nr_rows = 0; nr_rows < @ViewBag.daysInMonth; nr_rows++)
{
  <tr>
        <td>@Html.TextBoxFor(m => m.Model1[nr_rows].Rozpoczecie, new { @class = "start", @type = "time" })</td>
        <td>@Html.TextBoxFor(m => m.Model1[nr_rows].Zakonczenie, new { @class = "end", @type = "time" })</td>
        <td>@Html.TextBoxFor(m => m.Model1[nr_rows].OdbiorGodzin, new { @class = "gethours", @type = "time" })</td>
  </tr>
}

问题:

  • 还有其他方法可以将这些数据从控制器发送到视图吗?

  • 我在这里做错了什么?

【问题讨论】:

  • 如果在返回的视图附近添加“卡片”会引发错误:控制器中的代码在哪里?
  • @Phong 更新,请指导我,因为我已经受了很长时间了

标签: c# asp.net asp.net-mvc entity-framework asp.net-core


【解决方案1】:

您正在传递 List&lt;Karta&gt; 而在 View 中期待 ParentView

所以你应该这样做

var karta = _ecpContext.Karta.Where(f => f.Miesiac == numerMiesiaca && f.Rok == numerRoku && f.Login == userName).OrderBy(x => x.Rok).ThenBy(x => x.Miesiac).ThenBy(x => x.DzMiesiaca).ToList();
var kartaModel = karta.Select(p => new Karta_Model 
               { 
                 Id = p.Id // Assign the rest of properties here 
               }).ToList();
var viewModel = new ParentView { Model1  = kartaModel  };

return PartialView("_ReturnView", viewModel);

【讨论】:

    猜你喜欢
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多