如何在ASP.Net MVC中将类的值从视图传递给控制器

时间:2017-03-14 13:03:09

标签: c# asp.net-mvc

我可以在此检索一个字符串,但是当我尝试检索一个类时,我遇到了异常错误。

Customer = new Customer()
{

    FirstName = Request.Form["FirstName"],
    LastName = Request.Form["LastName "],
    BillingAddress = new Address
    {
        StreetAddress1 = Request.Form["StreetAddress1"],

    }

我已经尝试过设置一个值,它会成功保存,但是当我从值异常中获取数据时会发生错误。就像我使用SDK中的模型一样。谢谢

查看表单

@model WebApplication16.Model.ParentModel

@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}



@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>ParentModel</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">

        <div class="form-group">
            @Html.LabelFor(model => model.Customer.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Customer.FirstName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Customer.FirstName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Customer.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Customer.LastName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Customer.LastName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Customer.BillingAddress.StreetAddress1, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Customer.BillingAddress.StreetAddress1, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Customer.BillingAddress.StreetAddress1, "", new { @class = "text-danger" })
          </div>
         </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Customer.BillingAddress.StreetAddress2, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Customer.BillingAddress.StreetAddress2, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Customer.BillingAddress.StreetAddress2, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Customer.BillingAddress.City, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Customer.BillingAddress.City, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Customer.BillingAddress.City, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Customer.BillingAddress.StateCode, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Customer.BillingAddress.StateCode, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Customer.BillingAddress.StateCode, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Customer.BillingAddress.Country, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Customer.BillingAddress.Country, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Customer.BillingAddress.Country, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Customer.BillingAddress.ZipCode, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Customer.BillingAddress.ZipCode, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Customer.BillingAddress.ZipCode, "", new { @class = "text-danger" })
            </div>
        </div>

            <div class="form-group">
                @Html.LabelFor(model => model.CreditCard.CreditCardNumber, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.CreditCard.CreditCardNumber, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.CreditCard.CreditCardNumber, "", new { @class = "text-danger" })
                </div>
            </div>


            <div class="form-group">
                @Html.LabelFor(model => model.CreditCard.ExpirationDate, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.CreditCard.ExpirationDate, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.CreditCard.ExpirationDate, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.CreditCard.Issuer, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.CreditCard.Issuer, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.CreditCard.Issuer, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Payment.Amount, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Payment.Amount, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Payment.Amount, "", new { @class = "text-danger" })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.Payment.Cvv, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Payment.Cvv, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Payment.Cvv, "", new { @class = "text-danger" })
                </div>
            </div>






            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
</div>
}

 <div>
@Html.ActionLink("Back to List", "Index")
</div>

2 个答案:

答案 0 :(得分:3)

简单回答:您无法在表单中发送复杂的数据类型。

对于您的示例,您只需发送&#34; StreetAddress1&#34;作为一个字符串(与&#34; FirstName&#34;和#34; LastName&#34;一起)并在您的控制器中构建Customer对象。

您的观点代码:

@model Customer

<div>
    @using (Html.BeginForm("Action", "Controller", FormMethod.Post))
    {
        @Html.TextBoxFor(m => m.FirstName)<br />
        @Html.TextBoxFor(m => m.LastName)<br />
        @Html.TextBoxFor(m => m.BillingAddress)<br />
        <button type="submit">Save</button>
    }
</div>


如果您遵循这种方法,MVC已经为您序列化了Customer对象:

[HttpPost]
public ActionResult Action(Customer c)
{
    string FullName = c.FirstName + " " + c.LastName
}

答案 1 :(得分:0)

在你的控制器中使用FormCollection类..这将使你获得所有已发布的值从使用..然后你可以通过键提取值从集合键..根据你从FormCollection获得的值创建新对象,只需添加一个FromCollection参数

public ActionResult (FormCollection F)
{

}