C# 在ASP NET MVC中从控制器操作传递到视图时,模型中缺少属性值
我有一个asp.net web应用程序,它将表格中的模型列表显示为局部视图。在控制器中,模型列表的模型中的所有属性都已填充并看起来正常。当我试图在局部视图上显示这些时,问题出现了,只填充了描述,没有id,没有图像字符串。。。我不知道发生了什么事 我的数据模型DataModelDto:C# 在ASP NET MVC中从控制器操作传递到视图时,模型中缺少属性值,c#,asp.net,asp.net-mvc,list,C#,Asp.net,Asp.net Mvc,List,我有一个asp.net web应用程序,它将表格中的模型列表显示为局部视图。在控制器中,模型列表的模型中的所有属性都已填充并看起来正常。当我试图在局部视图上显示这些时,问题出现了,只填充了描述,没有id,没有图像字符串。。。我不知道发生了什么事 我的数据模型DataModelDto: namespace aspnet_client.Models { /// <summary> /// The DataModel data transfer object class.
namespace aspnet_client.Models
{
/// <summary>
/// The DataModel data transfer object class.
/// </summary>
public class DataModelDto
{
/// <summary>
/// Gets or sets the identifier.
/// </summary>
/// <value>
/// The identifier.
/// </value>
public int Id { get; set; }
/// <summary>
/// Gets or sets the description.
/// </summary>
/// <value>
/// The description.
/// </value>
public string Description { get; set; }
/// <summary>
/// Gets or sets the image.
/// </summary>
/// <value>
/// The image.
/// </value>
public string Image { get; set; }
}
}
namespace aspnet\u client.Models
{
///
///DataModel数据传输对象类。
///
公共类DataModelDto
{
///
///获取或设置标识符。
///
///
///标识符。
///
公共int Id{get;set;}
///
///获取或设置描述。
///
///
///描述。
///
公共字符串说明{get;set;}
///
///获取或设置图像。
///
///
///形象。
///
公共字符串图像{get;set;}
}
}
这是我的控制器操作:
using aspnet_client.Models;
using System.Collections.Generic;
using System.Web.Mvc;
namespace aspnet_client.Controllers
{
public class RecordsController : Controller
{
public RecordsController()
{
}
// GET: Records
public ActionResult Index()
{
return View();
}
public ActionResult _Records(List<DataModelDto> records)
{
if (records != null)
{
return PartialView(records);
}
else return null;
}
}
}
使用aspnet_client.Models;
使用System.Collections.Generic;
使用System.Web.Mvc;
命名空间aspnet_client.Controllers
{
公共类记录控制器:控制器
{
公共记录控制器()
{
}
//获取:记录
公共行动结果索引()
{
返回视图();
}
公共行动结果记录(列表记录)
{
if(记录!=null)
{
返回部分视图(记录);
}
否则返回null;
}
}
}
这是我的部分观点:
@model List<aspnet_client.Models.DataModelDto>
<h2>Registros actualizados en tiempo real</h2>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">Descripción</th>
<th scope="col">Imagen</th>
</tr>
</thead>
<tbody>
@foreach (var record in Model)
{
<tr>
<td>@record.Id</td>
<td>@record.Description</td>
<td>@record.Image</td>
</tr>
}
</tbody>
</table>
@型号列表
蒂恩波皇家酒店
身份证件
描述
图像
@foreach(模型中的var记录)
{
@记录.Id
@记录。描述
@记录,图像
}
我从websocket服务类调用records partial view(虽然可能不是最佳实践…),因此当服务器中的某些数据更新时,此控制器操作将接收更新的模型:
using aspnet_client.Controllers;
using aspnet_client.Models;
using Newtonsoft.Json;
using System.Collections.Generic;
using WebSocketSharp;
namespace aspnet_client
{
/// <summary>
/// The WebSocketService class
/// </summary>
public class WebSocketService
{
public RecordsController controller = new RecordsController();
/// <summary>
/// Initializes a new instance of the <see cref="WebSocketService"/> class.
/// </summary>
public WebSocketService()
{
}
/// <summary>
/// Starts the connection with the server.
/// </summary>
public void StartConnection()
{
var ws = new WebSocket("ws://127.0.0.1:4649/service");
var records = new List<DataModelDto>();
ws.OnMessage += (sender, e) =>
{
System.Diagnostics.Debug.WriteLine("Message received in the client side: " + e.Data);
records = JsonConvert.DeserializeObject<List<DataModelDto>>(e.Data);
controller._Records(records);
};
ws.Connect();
}
}
}
使用aspnet\u客户端控制器;
使用aspnet_client.Models;
使用Newtonsoft.Json;
使用System.Collections.Generic;
使用WebSocketSharp;
名称空间aspnet_客户端
{
///
///WebSocketService类
///
公共类WebSocketService
{
public RecordsController=new RecordsController();
///
///初始化类的新实例。
///
公共WebSocketService()
{
}
///
///启动与服务器的连接。
///
公共无效StartConnection()
{
var ws=newwebsocket(“ws://127.0.0.1:4649/service”);
var记录=新列表();
ws.OnMessage+=(发送方,e)=>
{
System.Diagnostics.Debug.WriteLine(“在客户端收到的消息:“+e.Data”);
records=JsonConvert.DeserializeObject(如数据);
控制器记录(记录);
};
ws.Connect();
}
}
}
任何帮助都将不胜感激,这对我来说是一种莫名其妙的感觉 您的控制器不是在传递值,而是在接收值。您如何调用
\u Records
?Hi@GSerg我已经更新了我的帖子,我从一个服务调用它,但这不应该是问题,因为数据被正确地传递到控制器操作,所有的道具都被填充了,出于某种原因,感觉它们在从控制器到视图的过程中迷失了方向控制器。\u Records(Records);代码>无效。它返回一个ActionResult
,然后将其丢弃。如果您想替换已经呈现的页面的一部分,那么可以使用javascript完成。但是您也不能这样做,因为newRecordsController()
创建了一个与任何请求处理管道都不相关的伪控制器实例,因此不清楚您想要做什么以及为什么。我还没有用javascript进行刷新,这将是我的下一步,我现在只想打印出值,这里的问题是,为什么只有一个属性被填充,而其他属性没有像我说的那样,使用javascript调用ActionResult\u Records()
,并使用返回的视图替换页面的一部分\u Records()
应该从您的服务中检索数据,而不是期望得到它。