Call procedure net7.0 Framework.

huangapple go评论78阅读模式
英文:

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&lt;IActionResult&gt; GetDetails(int userId)
{
    var userIdParameter = new SqlParameter(&quot;@UserId&quot;, userId);
    var query = $&quot;exec GetSingleUserRecord @UserId&quot;;
    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&lt;IActionResult&gt; GetDetails(int userId)
{
    var userIdParameter = new SqlParameter(&quot;@UserId&quot;, userId);
    var query = $&quot;exec GetSingleUserRecord @UserId&quot;;
    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&lt;IActionResult&gt; GetDetails(int userId)
{
    var userIdParameter = new SqlParameter(&quot;@UserId&quot;, userId);
    var query = $&quot;exec GetSingleUserRecord @UserId&quot;;
    var getSingleData = await _db.Users
        .FromSqlRaw(query, userIdParameter)
        .ToListAsync();

    return View(getSingleData);
}

And the view page should as below:

@model List&lt;Customer_Statements.Models.UserEntity&gt;

huangapple
  • 本文由 发表于 2023年6月13日 18:31:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76463973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定