当我尝试在另一个视图中显示文本框值时,下面的代码不起作用。我只获取默认值。我使用的是mvc5

时间:2016-11-28 04:15:02

标签: html asp.net-mvc razor

以下是我的观看代码。从此返回的客户类对象为null。我是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>

从文本框中添加控制器代码,型号代码和查看显示值代码。

型号代码

public class Customer
{
    public string customerName;
    public int customerId;
    public decimal amount;
}

文本框值显示代码

<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);
}

1 个答案:

答案 0 :(得分:0)

您必须使用相同的模型添加动作:

//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 !!!