简体   繁体   中英

Values from model not being passed back to view - .NET

I have the following form in a .NET Core application, consisting of a text input field and a "Submit" button.

I'd like the text from the text input field to re-appear in the form after submission, being passed to the controller action from the form and then being passed back to the view.

However, when I test the application, although the inputted values from the view appear when they are bound to the model in the controller, when they are passed back to the view they are wiped and I receive an "Object reference set to null" exception error.

I wondered if there's something missing from my code or what the potential cause of this may be?

Any advice would be great here,

Thanks,

Robert

// This is my view, featuring a simple form

在此处输入图像描述

// Values from the view are successfully being passed into the Controller

在此处输入图像描述

// This is the exception I receive when the values are passed back to the view: 在此处输入图像描述

My code:


    @page
    @model Models.StringModel
    
    
    <div class="text-left">
        
        <form asp-controller="Home" asp-action="Alter">
            <span class="form-control">
                <label asp-for="Name">Alter string:</label>
                @if (@Model != null)
                {
                    <input type="text" asp-for="Name" class="changeString" value="@Model.Name"/>
                } else
                {
                    
                    <input type="text" asp-for="Name" class="changeString"/>
                }
    
    
                <input class="btn btn-primary" type="submit" value="Update" action="Update" />
            </span>
        </form>
    </div>

StringModel.cs


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace SplitString.Models
    {
        public class StringModel
        {
            public int ID { get; set; }
    
            public string Name { get; set; }
        }
    }


HomeController.cs


    using Microsoft.AspNetCore.Mvc;
    using SplitString.Models;
    using System.Threading.Tasks;
    
    namespace SplitString.Controllers
    {
        public class HomeController : Controller
        {
            public IActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            [ValidateAntiForgeryToken]
            public async Task<IActionResult> Alter([Bind("Id, Name")] StringModel stringModel)
            {
    
                stringModel.ID = 1;
                
                return View("~/Pages/Index.cshtml", stringModel);
            }
        }
    }

Thanks,

Robert

By looking at this part of your code:

@if (@Model != null)
{
   <input type="text" asp-for="Name" class="changeString" value="@Model.Name"/>
}

I saw that you are trying to access the Name property without checking if it has any value.

If you change your code to this it shouldn't throw an exception anymore.

@if (@Model != null && !string.IsNullOrEmpty(Model.Name))
{
   <input type="text" asp-for="Name" class="changeString" value="@Model.Name"/>
}

please remove @page from your.cshtml file. If you use View Engine to render cshtml, please don't use @page. If you want to use @page, please use razor pages.

 // @page
 @model Models.StringModel
    
    
    <div class="text-left">
        
        <form asp-controller="Home" asp-action="Alter">
            <span class="form-control">
                <label asp-for="Name">Alter string:</label>
                @if (@Model != null)
                {
                    <input type="text" asp-for="Name" class="changeString" value="@Model.Name"/>
                } else
                {
                    
                    <input type="text" asp-for="Name" class="changeString"/>
                }
    
    
                <input class="btn btn-primary" type="submit" value="Update" action="Update" />
            </span>
        </form>
    </div>

It looks like you are using a razor page project,so that you don't need to use mvc in it.You only need to use page handler:

Index.cshtml:

 @page
    @model IndexModel
    
    
    <div class="text-left">
        
       <form method="post">
            <span class="form-control">
                <label asp-for="stringModel.Name">Alter string:</label>
                @if (@Model != null)
                {
                    <input type="text" asp-for="stringModel.Name" class="changeString" value="@Model.stringModel.Name"/>
                } else
                {
                    
                    <input type="text" asp-for="stringModel.Name" class="changeString"/>
                }
    
    
                <input class="btn btn-primary" type="submit" value="Update" action="Update" />
            </span>
        </form>
    </div>

Index.cshtml.cs:

public class IndexModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;
        [BindProperty]
        public StringModel stringModel { get; set; } = new StringModel();

        public IndexModel(ILogger<IndexModel> logger)
        {
            _logger = logger;
        }

        public void OnGet()
        {

        }
        public void OnPost()
        { 
             stringModel.ID = 1;
            //you can do something here
        }
    }

result: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM