Note that there are some explanatory texts on larger screens.

plurals
  1. POSending an object from javascript to an action method in MVC
    text
    copied!<p>I'm trying to send an object from razor view to an action method</p> <p>my view</p> <pre><code> &lt;section&gt; &lt;script type="text/javascript"&gt; function saveExpense() { var expenseobject = { date:$('.txtDate').val() , type:$('.ExpenseType').val() , cost: $('.cost').val(), extra:$('.extra').val() }; $.ajax({ // url: baseUri+'HomeController/saveexpense', url: '@Url.Action("saveexpense", "HomeController")', type: 'POST', contentType: 'application/json', data: JSON.stringify({ obj: expenseobject }), success: function (result) { } }); } &lt;/script&gt; &lt;section id="form"&gt; &lt;table width="600"&gt; &lt;tr&gt; &lt;td&gt;Select Date:&lt;/td&gt; &lt;td&gt; &lt;input class="txtDate" type="date" size="20"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Select Expense Type:&lt;/td&gt; &lt;td&gt; &lt;select class="ExpenseType"&gt; &lt;optgroup label="Room"&gt; &lt;option&gt;Room Fare&lt;/option&gt; &lt;/optgroup&gt; &lt;optgroup label="Mess"&gt; &lt;option&gt;Monthly Mess&lt;/option&gt; &lt;/optgroup&gt; &lt;optgroup label="Others"&gt; &lt;option&gt;Bus Fare&lt;/option&gt; &lt;option&gt;Tapari&lt;/option&gt; &lt;option&gt;Mobile Recharge&lt;/option&gt; &lt;option&gt;Auto&lt;/option&gt; &lt;/optgroup&gt; &lt;/select&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Enter Cost:&lt;/td&gt; &lt;td&gt; &lt;input class="cost" type="text" size="45" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Extra Details:&lt;/td&gt; &lt;td&gt; &lt;input class="extra" type="text" size="45" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt; &lt;button onClick="saveExpense();" &gt;Submit&lt;/button&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/section&gt; &lt;/section&gt; </code></pre> <p>And this is my controller</p> <pre><code> public ActionResult saveexpense(Expense obj) { obj.ExpenseId = Guid.NewGuid(); Debug.Print(obj.cost.ToString()); if (ModelState.IsValid) { context.expenses.Add(obj); context.SaveChanges(); int total = context.expenses.Sum(x =&gt; x.cost); return Json(new { spent = total, status = "Saved" }); } return Json(new { status = "Error" }); } </code></pre> <p>Where it leaves in the HomeController.cs</p> <p>when I inspect the response, I find</p> <p>[HttpException]: The controller for path &#39;/HomeController/saveexpense&#39; was not found or does not implement IController.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload