Html 当我试图在另一个视图中显示文本框值时,下面的代码不起作用。I';我只得到默认值。I';我使用mvc5

Html 当我试图在另一个视图中显示文本框值时,下面的代码不起作用。I';我只得到默认值。I';我使用mvc5,html,asp.net-mvc,razor,Html,Asp.net Mvc,Razor,下面是我的视图代码。从这里返回的客户类对象为空。我是mvc的初学者 @model WebApplication2.Models.Customer @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>InputCustomerWithHelper&

下面是我的视图代码。从这里返回的客户类对象为空。我是mvc的初学者

@model WebApplication2.Models.Customer
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>InputCustomerWithHelper</title>
</head>
<body>
    <div> 
        @using(Html.BeginForm("DisplayCustomerWith","First",FormMethod.Post))
        {
            <p>Customer Name    @Html.TextBoxFor(n=>n.customerName)</p>
            <p>Customer ID   @Html.TextBoxFor(n=>n.customerId)</p>
            <p> Amount to be Paid  @Html.TextBoxFor(n=>n.amount)</p>
            <input type="submit" value="Submit Data" />
        }
    </div>
</body>
</html>
文本框值显示代码

<div> 
    Customer Name : @Model.customerName
    Customer ID : @Model.customerId
    Amount : @Model.amount
</div>

您必须添加具有相同模型的操作:


从这里返回为空是什么意思<代码>空在哪里?在POST方法中?在视野中?显示相关代码(模型和控制器方法)@Stephen:我已经添加了代码。当我调试时,我可以在客户obj(控制器代码)中看到空值。您的
客户
不是
-它的字段值是
DefaultModelBinder
不绑定字段,只绑定属性。将它们设置为所有属性-
publicstringcustomername{get;set;}
,它将正常工作。非常感谢你。
<div> 
    Customer Name : @Model.customerName
    Customer ID : @Model.customerId
    Amount : @Model.amount
</div>
public ActionResult InputCustomerWithHelper()
{
    return View();
}
[HttpPost]
public ActionResult DisplayCustomerWith(Customer obj)
{
    return View("LoadCustomerData",obj);
}
//Action should be POST method "DisplayCustomerWith" and Controller will be "First" with "Customer" model
[HttpPost]
public ActionResult DisplayCustomerWith(Customer objCustomer)
{
return view();
}

Hope this will help you !!!