ASP.NET MVC:将字符串传递给视图

时间:2022-11-30 17:42:49

I have a controller that looks like this:

我有一个看起来像这样的控制器:

public class PersonController : Controller
{
    public ActionResult Result()
    {
        var s = new PersonResult();
        s = GetPerson(ViewBag.PersonInfo);
        return View(s);
    }
[...]
}

The controller calls the view Result which looks like this:

控制器调用视图Result,如下所示:

@model My.Class.Library.DTO.PersonController
@if (Model != null && Model.Persons.Count > 0)
{
     @Html.Partial("Persons", @Model.Persons)
}

So the model can hold many Persons. Persons is sent to its own view like this (view Persons):

因此该模型可以容纳许多人。人员被发送到这样的观点(查看人员):

@using My.Class.Library.DTO

@model List<Person>

<section>
    @foreach (Person person in @Model)
    {
        @Html.Partial("Person", person)
    }
</section>

So I'm sending each Person person to my view Person. And in that view I'm drawing each Person like so:

所以我将每个人发送给我的观点人员。在那个视图中,我正在绘制每个人:

@model Person
@if (Model.Fields.TryGetValue("description", out description)
{
    var descSplit = description.Split('#');
    foreach (string s in descSplit)
    {
        <div class="row-fluid">
            <div class="span2">Person</div>
            <div class="span10">@s</div>
        </div>
    }
}

But instead of doing that, I want to pass the string s to its own view. Something like this:

但我没有这样做,而是希望将字符串s传递给自己的视图。像这样的东西:

@model Person
@if (Model.Fields.TryGetValue("description", out description)
{
    var descSplit = description.Split('#');
    <section>
        @foreach (string s in descSplit)
        {
            @Html.Partial("Description", s)
        }
   </section>        
}

But "s" is just a primitive type: a string. How do I pass that to my view "Description"? What should my view "Description" look like? I'm thinking something like this:

但“s”只是一种原始类型:一个字符串。如何将其传递给我的“描述”视图?我的观点“描述”应该是什么样的?我在想这样的事情:

@model string

    <div class="row-fluid">
        <div class="span2"><b>TEST</b></div>
        <div class="span10">@s</div>
    </div>

But that's not correct... What should my model be and how can I present the string (s) that I'm sending from the other view?

但那不正确......我的模型应该是什么?如何呈现我从另一个视图发送的字符串?

1 个解决方案

#1


1  

Your code looks right but in your partial view, try using the Model property.

您的代码看起来正确,但在部分视图中,请尝试使用Model属性。

@model string

    <div class="row-fluid">
        <div class="span2"><b>TEST</b></div>
        <div class="span10">@Model</div>
    </div>

When you strongly type your Views/PartialViews, you have to use the Model property to read the value you have passed as a Model to this View/PartialView.

当您强烈键入Views / PartialViews时,必须使用Model属性将作为Model传递的值读取到此View / PartialView。

#1


1  

Your code looks right but in your partial view, try using the Model property.

您的代码看起来正确,但在部分视图中,请尝试使用Model属性。

@model string

    <div class="row-fluid">
        <div class="span2"><b>TEST</b></div>
        <div class="span10">@Model</div>
    </div>

When you strongly type your Views/PartialViews, you have to use the Model property to read the value you have passed as a Model to this View/PartialView.

当您强烈键入Views / PartialViews时,必须使用Model属性将作为Model传递的值读取到此View / PartialView。