How can I get a double from the View into My Model?

前提是你 提交于 2020-01-17 03:14:13

问题


This is weird and I'm being on this for a couple days with no more ideas.

Even asked before with no results as well

What's the way to add decimal number to SQL that replicate in EF?

I have my simple Model like this

public class SalesModelView
{
    public int SaleId { get; set; }
    public int ShopId { get; set; }

    public DateTime SaleDate { get; set; }

    public int Hours { get; set; }
    public double Sales { get; set; }
    public double Treatments { get; set; }

    public double Turnover { get; set; }
    public double TurnoverHour { get; set; }
    public double Efficiency { get; set; }
}

And the HTML is scaffolded as Create containing the trouble input as

<div class="editor-label">
    @Html.LabelFor(model => model.Sales)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Sales)
    @Html.ValidationMessageFor(model => model.Sales)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.Treatments)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Treatments)
    @Html.ValidationMessageFor(model => model.Treatments)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.Turnover)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Turnover)
    @Html.ValidationMessageFor(model => model.Turnover)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.TurnoverHour)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.TurnoverHour)
    @Html.ValidationMessageFor(model => model.TurnoverHour)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.Efficiency)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Efficiency)
    @Html.ValidationMessageFor(model => model.Efficiency)
</div>

When I try to add an integer, it assumes correctly, but when I add a decimal value, it just won't pass back into my controller.

What am I missing?

I even tried to set the CultureInfo to the correct country, but no luck

JQuery Validation works fine btw.

original image for better viewing

http://www.balexandre.com/temp/2011-05-28_1046.png


回答1:


You will have to write a custom model binder.

See http://haacked.com/archive/2011/03/19/fixing-binding-to-decimals.aspx



来源:https://stackoverflow.com/questions/6160557/how-can-i-get-a-double-from-the-view-into-my-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!