2017-06-06 78 views
1

有這個類:無法從在.NET核心雙值Web表單價值

public class ClientViewModel 
{ 
    public long PKID { get; set; } 
    [Required] 
    public string Name { get; set; } 
    public string Unit { get; set; } 
    public string StreetNumber { get; set; } 
    public string Street { get; set; } 
    public string Suburb { get; set; } 
    public string State { get; set; } 
    public string PostCode { get; set; } 
    public string Country { get; set; } 
    public string DT_RowId { get; set; } 
    public double Longitude { get; set; } 
    public double Latitude { get; set; } 
    public bool IsActive { get; set; } 
} 

而這種代碼的Razor視圖:

<form method="post" asp-controller="Clients" asp-action="Edit"> 
    <input asp-for="PKID" type="hidden" /> 

    <div class="portlet-body"> 
     <div class="row"> 
      <div class="form-group col-md-2"> 
       <label asp-for="Name">Name:</label> 
       <input class="form-control input-sm" type="text" placeholder="Name" asp-for="Name"> 
       <span class="help-block has-error" asp-validation-for="Name"></span> 
      </div> 
     </div> 
     <div class="row"> 
      <div class="col-md-6"> 
       <div class="row physical-row default-row"> 
        <div class="col-md-2"> 
         <label class="control-label">Unit</label> 
         @Html.TextBoxFor(model => model.Unit, new {placeholder = "Unit", id = "physicalLocationUnit", @class = "form-control input-sm"}) 
        </div> 
        <div class="col-md-4"> 
         <label class="control-label">Street</label> 
         @Html.TextBoxFor(model => model.Street, new {placeholder = "PhysicalAddress", id = "physicalLocation", onfocus = "physicalinitialize()", @class = "form-control input-sm"}) 
        </div> 
        <div class="col-md-2 postcode-box"> 
         <label class="control-label">Post Code</label> 
         <div> 
          <input type="text" 
            tabindex="-1" 
            class="form-control input-sm" /> 
         </div> 
        </div> 
        <div class="col-md-2 lattitude-box"> 
         <label class="control-label" asp-for="Latitude">Latitude</label> 
         <div> 
          <input type="text" 
            tabindex="-1" 
            class="form-control input-sm" asp-for="Latitude" /> 
         </div> 
        </div> 
        <div class="col-md-2 longitude-box"> 
         <label class="control-label" asp-for="Longitude">Longitude</label> 
         <div> 
          <input type="text" 
            tabindex="-1" 
            class="form-control input-sm" asp-for="Longitude" /> 
         </div> 
        </div> 
       </div> 
      </div> 
      <div class="form-actions"> 
        <div class="col-md-offset-6 col-md-6"> 
         <button type="submit" class="btn green">Save</button> 
         <a asp-controller="Clients" asp-action="WorkManage" asp-route-id="@Model.PKID" class="btn green">Add Work</a> 
        </div> 
      </div> 
     </div> 
    </div> 
</form> 

在經度和緯度具有價值示例1.32.45但是當我嘗試調用控制器方法時,我只在這個變量中有0值。我錯在哪裏? 乾杯!

+0

當經度和緯度的值例如'1,3'和'2,45'它的作品。但是,當js驗證程序不能保存這個,因爲想看到'1.3'和'2.45' –

回答

1

它不能使用一段時間的原因是服務器上的默認CultureInfo只接受逗號作爲小數點分隔符。

您可能想要嘗試將默認文化設置爲期望爲小數點分隔符的文化。

你可以做,在你的startup.cs,像這樣:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, FinOsDbContext context) 
{ 
    app.UseRequestLocalization(); 

    CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("*insert culture*"); 
    CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("*insert culture*"); 
} 

如果你總是打算使用一段作爲小數點分隔符,不管是文化的默認的,你可以指定小數點文化應該使用像這樣:

System.Globalization.CultureInfo customCulture = new CultureInfo("*insert culture*"); 
customCulture.NumberFormat.NumberDecimalSeparator = "."; 

CultureInfo.DefaultThreadCurrentCulture = customCulture; 
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo; 
+0

Asp.Net有一些非常廣泛的文化支持。你甚至可以指定多個支持的區域性和使用每個請求的定位(http://www.jerriepelser.com/blog/setting-thread-culture-aspnet5/) –

+0

嘗試插入startup.cs你的代碼,但HASE錯誤,因爲沒」 t看到這個方法'CurrentUICulture'。你確定.net核心? –

+0

對不起,看起來我錯了......掛在 –