-1

I'm using following code in a SharePoint provider hosted MVC app to hit a controller method

var urlAddProduct = "/Home/AddProduct?SPHostUrl=" + spHostUrl;
$.post(urlAddProduct,
        {
          id: id            
        }).done(function () {

        }).fail(function () {
             alert("Failed to add the new product!");
 });
 //method
  [SharePointContextFilter]
 public ActionResult AddProduct(string id) {
    var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
    using(var clientContext = spContext.CreateUserClientContextForSPHost()) {
    }
  return View();
}

Code is working fine but always return in failed state/failed alert.I am not getting any code exception.

user32124
  • 23
  • 8
  • Replace the `fail` function with `.fail(function(xhr, status, error) { /* check error */ });` and check the error it passes into the function. – martennis Jul 21 '16 at 10:11
  • error says "AddProduct its master was not found no view engine supports . the following locations were searched...". I have a button inside index.cshtml and on button click ,i am running the code – user32124 Jul 21 '16 at 11:21

1 Answers1

0

According to your comment, check to see if you have a view in your solution under path /Views/Home/AddProduct.cshtml

martennis
  • 882
  • 11
  • 20
  • I do not want to add a view ,it is a just button inside index.cshtml and click on button I want to call a Action method inside home controller.Isn't /Home/AddProduct try to call AddProduct action and it is calling though but with return as view error – user32124 Jul 21 '16 at 11:43
  • So you want to call the action to perform some functionality, but you don't require any response about the result? Instead of `return View();` use `return new EmptyResult();` – martennis Jul 21 '16 at 12:07
  • Thanks ,got it ,this is working.But what If I want to use return view() with setting some data ViewData["abc"] ="text" and returning view() ,how can I avoid this error while using same index.cshtml view.SInce the button is on same view index.cshtml it is supposed to return view.Please correct me – user32124 Jul 21 '16 at 12:48
  • Best way to do that is to return a JSON object using something like `return new JsonResult(new { abc = "abc"});` and handle the response on the client using Javascript/Jquery – martennis Jul 21 '16 at 12:50