Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将传递的字符串参数引用到局部视图_C#_Asp.net_Model View Controller_Controller_Partial Views - Fatal编程技术网

C# 将传递的字符串参数引用到局部视图

C# 将传递的字符串参数引用到局部视图,c#,asp.net,model-view-controller,controller,partial-views,C#,Asp.net,Model View Controller,Controller,Partial Views,我想将字符串变量传递给局部视图,但我不确定如何向局部视图显示字符串参数。我尝试了一些类似问题的答案,但得到了以下结果: “我的应用程序。模型。狗疗法”。有人能告诉我为什么吗 这是我的密码: 控制器: [HttpPost] public ActionResult CasIndex(int Sid) { string treat = dbContext.DogTreatments.Where(x => x.Sid == Sid).SingleOrDefault

我想将字符串变量传递给局部视图,但我不确定如何向局部视图显示字符串参数。我尝试了一些类似问题的答案,但得到了以下结果:

“我的应用程序。模型。狗疗法”。有人能告诉我为什么吗

这是我的密码:

控制器:

[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")
局部视图:

@model string

@{
    Layout = null;
}

@Model

你所看到的是正确的,因为你的LINQ声明

string treat = dbContext.DogTreatments.Where(x => x.Sid == Sid).SingleOrDefault().ToString();
dbContext.dogtherations.Where(x=>x.Sid==Sid)
过滤所有dogtherations
Where
x.Sid==Sid

.SingleOrDefault()
选择类型为
DogTreatments
或默认值(null)的
单个对象

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

也许这能满足您的要求:

从LINQ查询返回对象:

var treat = dbContext.DogTreatments.Where(x => x.Sid == Sid).SingleOrDefault();
return PartialView("DisplayTreatments", treat);
局部视图将如下所示:

@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
   }

如果我的回答有所帮助,请考虑接受。谢谢