0

I have a problem in ajax and ASP.NET MVC. When I click on button submit, I redirect to a partial page. I don't need redirect - I need replace div

Controller

public PartialViewResult ajaxtest(string prname)
{
    var aj = db.acount.Where(p => p.name.StartsWith(prname));
    return PartialView("_ajTest", aj.ToList());
}

View

@using (Ajax.BeginForm("ajaxtest", "Default1", new AjaxOptions () {
     HttpMethod = "POST",
     UpdateTargetId = "othmanN",
     InsertionMode = InsertionMode.Replace 
}))
{
   <label>Product Name :</label>
   @Html.TextBox("prname")

   <input type="submit" name="name" value="search" />
}
<div id="othmanN">
//here table 
</div>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • did you tried using JQuery , and just replace Html in success function ? – Ahmed Ghazey Dec 09 '18 at 15:04
  • you can return partial view as string using this url https://stackoverflow.com/questions/4730777/mvc-return-partial-view-as-json – Ahmed Ghazey Dec 09 '18 at 15:06
  • i dont need convert to string – Othman Jadallah Dec 09 '18 at 15:27
  • when it return as string you can use html string into html -replace div - $("container selector").html(returned html); – Ahmed Ghazey Dec 09 '18 at 15:48
  • Did you include `jquery.unobtrusive-ajax.js` javascript file in your page ? If you are seeing a normal form submit instead of ajax, that means the needed scripts are not properly loaded. Check your browser console and see whether you are seeing any errors there. – Shyju Dec 09 '18 at 19:22
  • See this https://stackoverflow.com/questions/39212624/passing-updatetargetid-in-ajax-beginform-not-replacing-the-target/39212725#39212725 – Shyju Dec 09 '18 at 19:22

1 Answers1

0

Your code is fine, just need to import jquery.unobrusive-ajax.js

Import after jquery import

<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>

If you don't have de lib instaled, it's here: https://www.nuget.org/packages/Microsoft.jQuery.Unobtrusive.Ajax/

Ivan Valadares
  • 869
  • 1
  • 8
  • 18