1

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:

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

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

@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
    Please, stop referring to "ASP.NET MVC" simply as "MVC". One is a framework, while other is a language-independent design pattern. It's like calling IE - "the internet" Commented Mar 22, 2013 at 11:10

1 Answer 1

3

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

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.