Atul Sureka
Atul Sureka

Reputation: 3312

How to pass decimal value to Action method

I have following Action methods

public ViewResult Index()
public ActionResult Edit(decimal id)

When user clicks on Edit link, I want to invoke the Edit Action. Following is the sample code snippet

<td>
    @Html.ActionLink("Edit", "Edit", new { id=item.ItemID }) |
    @Html.ActionLink("Details", "Details", new { id=item.ItemID }) |
    @Html.ActionLink("Delete", "Delete", new { id= item.ItemID})
</td>

The URL it redirects to is http://MyServer/Orders/Details/0.020

With this URL my action method does not invoke. If I manually edit the URL to remove "." then my method gets call.

My question is what is the right way to pass the decimal value to invoke the Action method?

Upvotes: 1

Views: 2578

Answers (4)

you can write type in View. Ive used that for any type and it works excelent.

id=(decimal) item.ItemID 

Upvotes: 0

Nilesh Gajare
Nilesh Gajare

Reputation: 6398

Add the following line to your site's web.config within the system.webServer / handlers element

<add name="ApiURIs-ISAPI-Integrated-4.0"
     path="*"
     verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
     type="System.Web.Handlers.TransferRequestHandler"
     preCondition="integratedMode,runtimeVersionv4.0" />

Upvotes: 0

Bhalchandra K
Bhalchandra K

Reputation: 2681

Try with custom Decimal Model Binder, a nice article by Phil Haack

abastract -

public class DecimalModelBinder : IModelBinder {
    public object BindModel(ControllerContext controllerContext, 
        ModelBindingContext bindingContext) {
        ValueProviderResult valueResult = bindingContext.ValueProvider
            .GetValue(bindingContext.ModelName);
        ModelState modelState = new ModelState { Value = valueResult };
        object actualValue = null;
        try {
            actualValue = Convert.ToDecimal(valueResult.AttemptedValue, 
                CultureInfo.CurrentCulture);
        }
        catch (FormatException e) {
            modelState.Errors.Add(e);
        }

        bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
        return actualValue;
    }
}

Upvotes: 2

Mark PM
Mark PM

Reputation: 2919

The best way to do that is to pass it using query string:

http://MyServer/Orders/Details?id=0.20

How did you define your routes? Remove the id from the route and ActionLink will add it as query string.

Upvotes: 3

Related Questions