模型中的值为null或空

时间:2012-10-16 14:41:35

标签: asp.net-mvc razor

我有这个动作方法

[HttpPost]
public ActionResult CreateEsf(EsfLotDetailsModel model)
{
   ...
}

我在这个模型中有两个属性。一个是数据库POCO对象,另一个是列表。在这个方法的GET等价物中,这些值都被正确填充,但是在帖子上这些值被设置为null(POCO)和空(列表)。

为什么会这样?

我的观点在这里

@using UI.Helpers

@model UI.Areas.Admin.Models.EsfLotDetailsModel

@{
    ViewBag.Title = "Create Forward Lot";

}

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Content/AdminDesignTheme/js/wl_Date.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Content/AdminDesignTheme/js/wl_Time.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.mousewheel.min.js")" type="text/javascript"></script>

@{
        <script type="text/javascript">
            $(document).ready(function () {
                var options = {
                    dateFormat: "dd/mm/yy"
                };
                $('#Starts').wl_Date(options);
                $('#Ends').wl_Date(options);
                $('#startTime').wl_Time();
                $('#endTime').wl_Time();
            });
        </script>
}

<p>@Html.ActionLink("Back to Item List", "Index","Inventory")</p>

@Html.ValidationSummary(true, "Please correct the errors and try again.")
@using (Html.BeginForm(new {model = @Model})) {

    @Html.HiddenFor(model => model.Auction.InventoryReference)
    @Html.HiddenFor(model => model.Auction.Title)
    @Html.HiddenFor(model => model.Auction.Description)
    <fieldset>
        <legend></legend>
        <label>ESF Lot Information</label>            

        <section>
            @Html.LabelFor(model => model.Auction.Title)
            <div> @Html.DisplayFor(model => model.Auction.Title) 
            @Html.ValidationMessageFor(model => model.Auction.Title)    </div>   
        </section>

        <section>
             @Html.LabelFor(model => model.Auction.Description)
             <div> @Html.DisplayFor(model => model.Auction.Description) 
             @Html.ValidationMessageFor(model => model.Auction.Description)     </div>  
        </section>

        @if (HttpContextHelper.IsUserAdmin())
        {<section>
             <label for="IsFeatured">Is Featured <i>(displayed in homepage)</i></label>
             <div> @Html.CheckBoxFor(model => model.Auction.IsFeatured) 
             @Html.ValidationMessageFor(model => model.Auction.IsFeatured)     </div>  
        </section>
        }
        else
        {
            <section>
             <label for="IsFeatured">Is Featured <i>(displayed in homepage)</i></label>
             <div> @Html.CheckBoxFor(model => model.Auction.IsFeatured, new { disabled = "disabled" }) (Contact Admin to make this a featured lot) 
             @Html.ValidationMessageFor(model => model.Auction.IsFeatured)     </div>  
            </section>

        }

        @if (HttpContextHelper.IsUserAdmin())
        {<section>
             @Html.Label("VAT Applicable")
             <div> @Html.CheckBoxFor(model => model.Auction.VatApplicable) 
             @Html.ValidationMessageFor(model => model.Auction.VatApplicable)     </div>  
        </section>
        }
        else
        {
            <section>
             @Html.Label("VAT Applicable")
             <div> @Html.CheckBoxFor(model => model.Auction.VatApplicable, new { disabled = "disabled" }) (Contact Admin if it is not VATable) 
             @Html.ValidationMessageFor(model => model.Auction.VatApplicable)     </div>  
            </section>

        }

    </fieldset>

    <fieldset>
        <legend></legend>
        <label>Date and Time</label>

        <section>
             <label>Starts <em>(dd/mm/yy hh:mm)</em></label>
             <div> <input type="text" class="date" id="Starts" /> 
                <input type="text" class="time" data-connect="Starts" id="startTime" /> 

             @Html.ValidationMessageFor(model => model.Auction.Starts)       </div>
        </section>

        <section>
             <label>Ends <em>(dd/mm/yy hh:mm)</em></label>
             <div> <input type="text" class="date" id="Ends" /> 
                   <input type="text" class="time" data-connect="Ends" id="endTime" /> 

             @Html.ValidationMessageFor(model => model.Auction.Ends)   </div>    
        </section>
        <section>
             @Html.LabelFor(model => model.Auction.IsExtensible)
             <div> @Html.CheckBoxFor(model => model.Auction.IsExtensible) 
             @Html.ValidationMessageFor(model => model.Auction.IsExtensible)     </div>  
        </section>
    </fieldset>
    <fieldset>
        <legend></legend>
        <label>Bid Options</label>

        <section>
             @Html.LabelFor(model => model.Auction.StartingBid)
             <div> @Html.TextBoxFor(model => model.Auction.StartingBid) 
             @Html.ValidationMessageFor(model => model.Auction.StartingBid)     </div>  
        </section>

        <section>
             @Html.LabelFor(model => model.Auction.Reserve)
             <div> @Html.TextBoxFor(model => model.Auction.Reserve) 
             @Html.ValidationMessageFor(model => model.Auction.Reserve)   </div>    
        </section>

        <section>
             <label>Reserve Visible <em>(Displays as Reserve met or not met)</em></label>
             <div> @Html.CheckBoxFor(model => model.Auction.ReserveVisible) 
             @Html.ValidationMessageFor(model => model.Auction.ReserveVisible)  </div>     
        </section>

        <section>
             @Html.LabelFor(model => model.Auction.IsBidIncrementPercentual)
             <div> @Html.CheckBoxFor(model => model.Auction.IsBidIncrementPercentual) 
             @Html.ValidationMessageFor(model => model.Auction.IsBidIncrementPercentual)     </div>  
        </section>  

        <section>
             @Html.LabelFor(model => model.Auction.BidIncrement)
             <div> @Html.TextBoxFor(model => model.Auction.BidIncrement, new { @Value = 1m })
             @Html.ValidationMessageFor(model => model.Auction.BidIncrement)        </div>
        </section>              

        <section>
             @Html.LabelFor(model => model.AuctionEvents)
             <div> @Html.DropDownList("Auction", Model.AuctionEvents, "Select auction", new { required = "required" }) 
             @Html.ValidationMessageFor(model => model.AuctionEvents)       </div>
        </section>

        <section>
            <div><button>Create</button></div>
        </section>        

    </fieldset>
}

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

3 个答案:

答案 0 :(得分:0)

将FormMethod = FormMethod.Post添加到您的Html.BeginForm()

@using (Html.BeginForm("Action", "Controller",FormMethod.Post))
{

}

答案 1 :(得分:0)

您的提交按钮在哪里?

@using (Html.BeginForm("Action", "Controller",FormMethod.Post))
{
  ...
  <input type="submit" value="submit" />
}

填写表格时,按提交按钮,然后输入[HttpPost]方法,将填写模型的值。

答案 2 :(得分:0)

这些选项都不起作用。我将POCO对象中的所有值分别放在模型中,并删除了POCO对象。不知道为什么它不起作用。