英文:
Call procedure net7.0 Framework
问题
抱歉,以下是您提供的代码的翻译部分:
亲们,我从C#代码中调用了sp
public async Task<IActionResult> GetDetails(int userId)
{
var userIdParameter = new SqlParameter("@UserId", userId);
var query = "exec GetSingleUserRecord @UserId";
var getSingleData = await _db.Users
.FromSqlRaw(query, userIdParameter)
.ToListAsync();
return View(getSingleData);
}
但是出现了错误:
在处理请求时发生了一个未处理的异常。
InvalidOperationException:传递到ViewDataDictionary的模型项类型为'System.Collections.Generic.List`1[Customer_Statements.Models.UserEntity]',但此ViewDataDictionary实例需要类型为'Customer_Statements.Models.UserEntity'的模型项。
'Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.EnsureCompatible(object value)'
这是我的第一个应用程序,请给我一些建议。
英文:
Dears, I call sp from code C#
public async Task<IActionResult> GetDetails(int userId)
{
var userIdParameter = new SqlParameter("@UserId", userId);
var query = $"exec GetSingleUserRecord @UserId";
var getSingleData = await _db.Users
.FromSqlRaw(query, userIdParameter)
.ToListAsync();
return View(getSingleData);
}
but get error:
>An unhandled exception occurred while processing the request.
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List`1[Customer_Statements.Models.UserEntity]', but this ViewDataDictionary instance requires a model item of type 'Customer_Statements.Models.UserEntity'.
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.EnsureCompatible(object value)
Its my first app, please advise me
答案1
得分: 1
To View the single record, you could modify the query statement as below:
public async Task<IActionResult> GetDetails(int userId)
{
var userIdParameter = new SqlParameter("@UserId", userId);
var query = "exec GetSingleUserRecord @UserId";
var getSingleData = await _db.Users
.FromSqlRaw(query, userIdParameter)
.FirstOrDefaultAsync();
return View(getSingleData);
}
Then in the view page, the page model should be like this:
@model Customer_Statements.Models.UserEntity
If you want to view the list of items, the query statement should be like this:
public async Task<IActionResult> GetDetails(int userId)
{
var userIdParameter = new SqlParameter("@UserId", userId);
var query = "exec GetSingleUserRecord @UserId";
var getSingleData = await _db.Users
.FromSqlRaw(query, userIdParameter)
.ToListAsync();
return View(getSingleData);
}
And the view page should be as below:
@model List<Customer_Statements.Models.UserEntity>
英文:
Whether you have already solved the problem? If not, is there any updated?
> my first procedure returns the entire list of Гsers. now when I want
> to view a single record,
To View the single record, you could modify the query statement as below:
public async Task<IActionResult> GetDetails(int userId)
{
var userIdParameter = new SqlParameter("@UserId", userId);
var query = $"exec GetSingleUserRecord @UserId";
var getSingleData = await _db.Users
.FromSqlRaw(query, userIdParameter)
.FirstOrDefaultAsync();
return View(getSingleData);
}
Then in the view page, the page model like this:
@model Customer_Statements.Models.UserEntity
If you want to view the list of items, the query statement should like this:
public async Task<IActionResult> GetDetails(int userId)
{
var userIdParameter = new SqlParameter("@UserId", userId);
var query = $"exec GetSingleUserRecord @UserId";
var getSingleData = await _db.Users
.FromSqlRaw(query, userIdParameter)
.ToListAsync();
return View(getSingleData);
}
And the view page should as below:
@model List<Customer_Statements.Models.UserEntity>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论