PDA

View Full Version : جدا سازی اعداد با کاما در حالت ویرایش اطلاعات



resident
چهارشنبه 04 فروردین 1395, 06:25 صبح
سلام. من یه فیلد به صورت زیر تعریف کردم:


[DisplayFormat(DataFormatString = "{0:#,##0}")]
public float TransportationCost { get; set; }


تو view هم:


@Html.TextBoxFor(model => model.TransportationCost, new { @class = "form-control", lang = "en" })


میخوام در حالتی که کاربر دکمه ویرایش رو میزنه مبلغ موجود در این فیلد سه رقم سه رقم با کاما جدا بشه.
DisplayFormat رو براش نوشتم اما اعمال نمیشه. چرا؟

hakim22
پنج شنبه 05 فروردین 1395, 16:26 عصر
برای اینکار باید از جاوا اسکریپت استفاده کنید.

adameh_bahal
جمعه 06 فروردین 1395, 09:36 صبح
ابتدا اسکریپت jquerypriceformat (http://jquerypriceformat.com)رو به پروژه خودتو اضافه کنید و بعد به صورت زیر ازش استفاده کنید:

<script src="~/Scripts/jquery.price_format.2.0.min.js"></script>
<script>
$('#TransportationCost').priceFormat({
prefix: '',
thousandsSeparator: ',',
insertPlusSign: ''
});
</script>


حال کلاس زیر رو به پروژه تون اضافه کنید:

using System;
using System.Globalization;
using System.Threading;
using System.Web.Mvc;


namespace WebApplication1
{
/// <summary>
/// How to register it in the Application_Start method of Global.asax.cs
/// ModelBinders.Binders.Add(typeof(float), new FloatBinder());
/// </summary>
public class FloatBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (bindingContext.ModelType == typeof(float) || bindingContext.ModelType == typeof(float?))
{
return bindFloat(bindingContext);
}
return base.BindModel(controllerContext, bindingContext);
}


private static object bindFloat(ModelBindingContext bindingContext)
{
var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingConte xt.ModelName);
if (valueProviderResult == null)
return null;


bindingContext.ModelState.SetModelValue(bindingCon text.ModelName, valueProviderResult);
float value;
var valueAsString = valueProviderResult.AttemptedValue == null ?
null : valueProviderResult.AttemptedValue.Trim();
if (string.IsNullOrEmpty(valueAsString))
return null;


if (!float.TryParse(valueAsString, NumberStyles.Any, Thread.CurrentThread.CurrentCulture, out value))
{
const string error = "عدد وارد شده معتبر نیست";
var ex = new InvalidOperationException(error, new Exception(error, new FormatException(error)));
bindingContext.ModelState.AddModelError(bindingCon text.ModelName, ex);
return null;
}
return value;
}
}
}


و همونطور که توی کامنت کلاس ذکرشده دستور


ModelBinders.Binders.Add(typeof(float), new FloatBinder());


رو به متد Application_Start فایل Global.asax.cs اضاقه کنید

resident
شنبه 14 فروردین 1395, 07:46 صبح
ممنون از وقتی که گذاشتید اما متاسفانه عمل نکرد.
من تو متدهای شما به جای float ، دابل (double) گذاشتم

adameh_bahal
شنبه 14 فروردین 1395, 14:57 عصر
ممنون از وقتی که گذاشتید اما متاسفانه عمل نکرد.
من تو متدهای شما به جای float ، دابل (double) گذاشتم

من تست کردم قرار دادم اگر خطای بک اند ندارید کنسول مرورگر رو بررسی کنید شاید خطای کلاینت سایدی دارید مثلا ترتیب اسکریپ ها ممکنه صحیح نباشه یا المنت به درستی سلکت نشده

resident
یک شنبه 15 فروردین 1395, 09:15 صبح
میشه کد زیر رو جوری نوشت که لازم نباشه برای تک تک فیلدها این کد رو بنویسیم؟ بگیم هر فیلدی که کلاسش price بود کد زیر روش اعمال بشه.

<script src="~/Scripts/jquery.price_format.2.0.min.js"></script>
<script>
$('#TransportationCost').priceFormat({
prefix: '',
thousandsSeparator: ',',
insertPlusSign: ''
});
</script>

adameh_bahal
یک شنبه 15 فروردین 1395, 13:32 عصر
میشه کد زیر رو جوری نوشت که لازم نباشه برای تک تک فیلدها این کد رو بنویسیم؟ بگیم هر فیلدی که کلاسش price بود کد زیر روش اعمال بشه.

<script src="~/Scripts/jquery.price_format.2.0.min.js"></script>
<script>
$('#TransportationCost').priceFormat({
prefix: '',
thousandsSeparator: ',',
insertPlusSign: ''
});
</script>







بله میشه:

<script src="~/Scripts/jquery.price_format.2.0.min.js"></script>
<script>
$('.price').priceFormat({
prefix: '',
thousandsSeparator: ',',
insertPlusSign: ''
});
</script>

اینجوری هم میشه :

<script src="~/Scripts/jquery.price_format.2.0.min.js"></script>
<script>
$('#id1,#id2,#id3').priceFormat({
prefix: '',
thousandsSeparator: ',',
insertPlusSign: ''
});
</script>


ولی کلاس بهتره
کلی هم میتونید مثلا المنت هایی رو سلکت کنید که id یا name شون شامل عبارت cost باشه که خب باید دقت کنید منحصر به فرد باشه و جای دیگه استفاده نشده باشه

resident
یک شنبه 15 فروردین 1395, 16:23 عصر
یه سوال دیگه:
الان آخر عدد دو رقم اعشار میزاره. چطور میشه کاری کرد که دو رقم اعشار رو نزاره

resident
یک شنبه 15 فروردین 1395, 16:25 عصر
یه سوال دیگه:
الان آخر عدد دو رقم اعشار میزاره. چطور میشه کاری کرد که دو رقم اعشار رو نزاره

پیداش کردم باید centsLimit: 0 بزاریم

resident
یک شنبه 15 فروردین 1395, 17:13 عصر
یه سوال دیگه.
این فقط روی inputجواب میده؟
اگخ بخوام داخل یه ستون از جدول ، قیمت رو با کاما جدا کنم کاری میشه کرد؟

adameh_bahal
دوشنبه 16 فروردین 1395, 13:18 عصر
از

String.Format("{0:N0}")

روی فیلد مورد نظر میتونید استفاده کنید