Search code examples
c#asp.net-coremodel.net-7.0

Values/Data dont show on View from Controller


I try send values/data from controller to view.

Model, classes EmailModel, EmailPersonModel

public class EmailModel
  {
      [Required]
      public EmailPersonModel Sender { get; set; } = new EmailPersonModel();
      [Required]
      public List<EmailPersonModel> Recipients { get; set; } = new List<EmailPersonModel>();
      [Required]
      public string Subject { get; set; }= string.Empty;
      [Required]
      public string Message { get; set; }= string.Empty;
}

public class EmailPersonModel
  {
      public string Email { get; set; } = string.Empty;
      public string Name { get; set; } = string.Empty;
}

Controller: i get data from service, values are in model

[HttpGet("SendInfoEmail/{id}")]
  public async Task<IActionResult> SendInfoEmail(Guid id)
  {
EmailModel? model =  await _emailsService.SendToUser(reportWithId, ADLogin).ConfigureAwait(false);

 return RedirectToAction("Index", model);
 }
public IActionResult Index(EmailModel model)
  {  

    return View("~/Views/Mails/Create.cshtml", model);
  }

View


<div class="container mt-5">

        <div class="mb-3">
            <label for="sender" class="form-label">Nadawca:</label>
            <input type="text"
                id="sender" 
                name="Sender.Email" 
                value="@Model.Sender.Email"
                class="form-control" 
                required />       
        </div>
        <div class="mb-3">
            <label for="recipients" class="form-label">Odbiorcy (oddziel przecinkami):</label>
            <input type="text" 
                id="recipients" 
                name="recipients" 
                value="@string.Join(", ", @Model.Recipients.Select(r => r.Email))"
                    class="form-control" 
                    required />
        </div>
        << --  others: subject + messageContent   -->>
    
</div>

Problem:

Value in Sender.Email and recipients dont display, but subject and messageContent display correctly


Solution

  • Value in Sender.Email and recipients dont display, but subject and messageContent display correctly

    Actually, you are using RedirectToAction which takes route value and action name as parameter as you can see below:

    enter image description here

    However, RedirectToAction cannot process complex object to pass your value within the controller action. In your request Recipients and Sender itself a complex model because its belongs to another model that is EmailModel and this is causing your null data on view. I have reproduce your issue accordingly:

    enter image description here

    How to resolve:

    Numerous way available to get rid of null data while redirect from one controller action to another.

    Directly calling action:

    Calling Index action directly and then we can pass the model value. Let's have a look in action.

    public async Task<IActionResult> SendInfoEmail()
            {
             
                EmailModel? model =
                    new EmailModel { Subject = "Test Subject", Sender =
                    new EmailPersonModel { Email = "[email protected]", Name = "sender" }, Message = "Test Message", Recipients =
                    new List<EmailPersonModel> {
                        new EmailPersonModel { Email = "[email protected]", Name = "Recipient1" },
                        new EmailPersonModel { Email = "[email protected]", Name = "Recipient2" } }
                    };
                return Index(model);
    
    
            }
    

    Index Action:

    public IActionResult Index(EmailModel model)
            {
    
                return View("~/Views/Mails/Create.cshtml", model);
            }
    

    Output:

    enter image description here

    Note: You can also use TempData to pass object in that scenario you would need to deserialize object/string into your strongly type model. For more details, you can refer to this example.