ZKF3340320
ZKF3340320

Reputation: 181

how to get ID from URL in asp.net MVC controller

I want to get the ID from URL in ASP.NET MVC Controller and insert it into Project_ID, the below is my code, I tried but it's not working for me.

http://localhost:20487/ProjectComponent/Index/1

My Controller

[HttpPost]
public JsonResult SaveComponent(OrderVM O, int id)
{        
    bool status = false;
    if (ModelState.IsValid)
    {
        using (Entities db = new Entities())
        {
            ProjComponent ProjComponent = new ProjComponent { project_id = id, title = O.title, description = O.description };
            foreach (var i in O.ProjComponentActivities)
            {
                ProjComponent.ProjComponentActivity.Add(i);
            }
            db.ProjComponents.Add(ProjComponent);
            db.SaveChanges();
            status = true;
        }
    }
}

Upvotes: 2

Views: 4442

Answers (2)

zohre harimi
zohre harimi

Reputation: 21

you can get the id from URL Like This:

Cotroller:

public ActionResult Index(int id)
{
    ViewBag.ID = id;
    Your Code......
    return View(...);
}

View:

@{  
ViewBag.Title = "Index";
var ID = ViewBag.ID;

} Now you have an ID in the variable

Upvotes: 2

Syed Mhamudul Hasan
Syed Mhamudul Hasan

Reputation: 1349

You can always use a hidden field and update it by jquery/javscript and send it to back end in ajax helper.....

Make sure 1.name should be exactly name as ActionMethod param and 3.Jquery ,jQuery Validate and jQuery unobstrusive ajax is loaded correctly

My code .cshtml

<script src="~/Scripts/jquery-2.1.4.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<div>
    @{
        AjaxOptions options = new AjaxOptions();
        options.HttpMethod = "POST";
        options.OnBegin = "OnBeginRequest";
        options.OnSuccess = "OnSuccessRequest";
        options.OnComplete = "OnCompleteRequest";
        options.OnFailure = "OnFailureRequest";
        //  options.Confirm = "Do you want to Add Country ?";
        options.UpdateTargetId = "divResponse";
        options.InsertionMode = InsertionMode.InsertAfter;
    }
    @using (Ajax.BeginForm("AjaxSend", "Stackoverflow", options))
    {

        <input type="hidden" name="project_id" id="project_id" value="project_id" />
        <input type="submit" value="Click me" />

    }
</div>
<div id="divResponse">
</div>
<script>
    $(function() {
        var url = window.location.href;
        var array = url.split('/');

        var lastsegment = array[array.length - 1];
        console.log(lastsegment);
        $('#project_id').val(lastsegment);

    });
    function OnBeginRequest() {
        console.log('On Begin');


    }
    function OnCompleteRequest() {
        console.log('On Completed');
    }
    function OnSuccessRequest() {
        console.log('On Success');

    }
    function OnFailureRequest() {
        console.log('On Failure');
    }
</script>

and Controller

    [HttpPost]
    public JsonResult AjaxSend(String project_id)
    {
        //rest goes here
        return Json(new { Success = true });
    }

this link may help link

Upvotes: 1

Related Questions