gpt4 book ai didi

c# - 将传递的字符串参数引用到分部 View

转载 作者:行者123 更新时间:2023-12-02 08:34:15 25 4
gpt4 key购买 nike

我想将字符串变量传递给分部 View ,但我不确定如何将字符串参数显示到分部 View 。我尝试了在类似问题上找到的一些答案,但得到了以下输出:

“my_app.Models.DogTreatments”。谁能告诉我这是为什么?

这是我的代码:

Controller :

[HttpPost]
public ActionResult CasIndex(int Sid)
{

string treat = dbContext.DogTreatments.Where(x => x.Sid == Sid).SingleOrDefault().ToString();




// ViewBag.TList = dbContext.DogTreatments.Where(x => x.Sid == Sid);

return PartialView("DisplayTreatments", treat);
}

查看页面:

@Html.Partial("~/Views/Shared/DisplayTreatments.cshtml")

部分 View :

@model string

@{
Layout = null;
}

@Model

最佳答案

由于您的 LINQ 语句,您所看到的内容是正确的。

string treat = dbContext.DogTreatments.Where(x => x.Sid == Sid).SingleOrDefault().ToString();

dbContext.DogTreatments.Where(x => x.Sid == Sid) 过滤所有 DogTreatments Where x.Sid == Sid

.SingleOrDefault()选择DogTreatments类型或default(null)的单个对象

toString() 会将对象类型转换为其字符串格式,因此 my_app.Models.DogTreatments

也许这会满足您的要求:

从 LINQ 查询返回对象:

var treat = dbContext.DogTreatments.Where(x => x.Sid == Sid).SingleOrDefault();
return PartialView("DisplayTreatments", treat);

部分 View 将如下所示:

@using my_app.Models.DogTreatments //(this might need to be fixed)
@model DogTreatments

@{
Layout = null;
}

// in here you can access the DogTreatments object
// These are just examples as I don't know from question what DogTreatments properties are
@if(Model != null)
{
@Model.Name
@Model.Treatment
}

关于c# - 将传递的字符串参数引用到分部 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60357514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com