2

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;
        }
    }
}
6
  • 1
    Your method is a POST, not a GET (you cannot navigate to a POST method). And the url navigates to the Index() method of ProjectComponentController, not to a method named SaveComponent Commented Sep 24, 2016 at 5:55
  • 2
    When I wear blue pants my car wont start. Commented Sep 24, 2016 at 5:57
  • dear Stephen Muecke, thanks for your comments, yes you are right i did a mistake on that. and i did not clearly described my problem. for details i posted many data through ajax in my controller accept project_id which is needed to be store in other table as foreign key. so how I can get the ID in my controller which uses Post Method from View in ASP.NET MVC?? Commented Sep 24, 2016 at 7:27
  • What method do you want to send ProjectID Ajax or From submit ?????? Commented Sep 24, 2016 at 8:05
  • I want to use Ajax Commented Sep 24, 2016 at 8:08

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.