How to pass parameters to a partial view in ASP.NET MVC?

后端 未结 6 1315
闹比i
闹比i 2020-11-29 00:45

Suppose that I have this partial view:

Your name is @firstName @lastName

which is accessible through a child o

6条回答
  •  孤城傲影
    2020-11-29 00:57

    You need to create a view model. Something like this should do...

    public class FullNameViewModel
    {
         public string FirstName { get; set; }
         public string LastName { get; set; }
    
         public FullNameViewModel() { } 
    
         public FullNameViewModel(string firstName, string lastName)
         {
              this.FirstName = firstName;
              this.LastName = lastName;
         }
    
    }
    

    then from your action result pass the model

    return View("FullName", new FullNameViewModel("John", "Doe"));
    

    and you will be able to access @Model.FirstName and @Model.LastName accordingly.

提交回复
热议问题