简体   繁体   English

模型中的值未传回视图 - .NET

[英]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.我在 .NET Core 应用程序中有以下表单,由文本输入字段和“提交”按钮组成。

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字符串模型.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家庭控制器.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.我看到您正在尝试访问 Name 属性而不检查它是否具有任何值。

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.请从您的.cshtml 文件中删除@page If you use View Engine to render cshtml, please don't use @page.如果您使用 View Engine 渲染 cshtml,请不要使用@page。 If you want to use @page, please use razor pages.如果你想使用@page,请使用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:看起来您正在使用剃刀页面项目,因此您不需要在其中使用 mvc。您只需要使用页面处理程序:

Index.cshtml:索引.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:索引.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:结果: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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