Sie sind auf Seite 1von 7

Google Facebook

OR

Learn, Share, Build Display name J. Doe

Each month, over 50 million developers come to Stack Overflow to


learn, share their knowledge, and build their careers. Email address you@example.com

Join the worlds largest developer community. Password ********

By registering, you agree to the privacy policy and terms of


Sign Up service.

How to use Simple Ajax Beginform in Asp.net MVC 4? [closed] Ask Question

asked 4 years, 5 months ago

viewed 128,233 times

active 11 months ago

I am new in Asp.net MVC and i researched about Ajax.BeginForm but when i apply codes it did
not work. Can you share very simple example with Ajax.Beginform with View, Controller, Model? BLOG
46 Thanks.
Why Channels?
ajax asp.net-mvc form-helpers ajax.beginform

share improve this question edited Feb 19 '15 at 21:22 asked Jun 13 '13 at 19:27 Linked
19 Amirhossein Mehrvarzi Soner Sevinc
5,480 3 26 45 559 2 6 25 252 Using Ajax.BeginForm with ASP.NET MVC

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
3 Razor

0 Display a bootstrap alert in a modal after


closed as too broad by S, Alok Patel, Ebbe M. Pedersen, Suever, Stephen Muecke Jul submitting a form
25 '16 at 0:55
0 MVC View not passing Model data to
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid
controller using Ajax.ActionLink
asking multiple distinct questions at once. See the How to Ask page for help clarifying this question.

If this question can be reworded to fit the rules in the help center, please edit the question. 0 MVC ASP.NET AJAX ACTION LINK
CREATE

5 MVC Partial Views issue


Let's see some code. X3074861X Jun 13 '13 at 20:17
-1 How to redirect to action in asp.net after
Check this link : stackoverflow.com/questions/5410055/ Bappi Datta Jun 13 '13 at 20:51 successful ajax request

add a comment 0 ajax html.BeginForm() post to controller

0 Asp.net mvc: how to send event back to


3 Answers active oldest votes server without page refresh

0 Popup message on MVC form submit

Simple example: Form with textbox and Search button. 0 Ajax call goes only one time ASP.Net
MVC5
73 If you write "name" into the textbox and submit form, it will brings you patients with "name" in
see more linked questions
table.

View:
Related
250 grid controls for ASP.NET MVC?
@using (Ajax.BeginForm("GetPatients", "Patient", new AjaxOptions {//GetPatients is name of method in PatientController
InsertionMode = InsertionMode.Replace, //target element(#patientList) will be replaced
UpdateTargetId = "patientList", 1537 Abort Ajax requests using jQuery
LoadingElementId = "loader" // div with .gif loader - that is shown when data are loading
})) 398 How can I properly handle 404 in ASP.NET
{ MVC?
string patient_Name = "";
545 How to get ELMAH to work with ASP.NET
@Html.EditorFor(x=>patient_Name) //text box with name and id, that it will pass to controller
<input type="submit" value="Search" /> MVC [HandleError] attribute?
}
559 ASP.NET MVC - Set custom IIdentity or
IPrincipal
@* ... *@
<div id="loader" class=" aletr" style="display:none">
298 How to use dashes in HTML-5 data-*

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Loading...<img src="~/Images/ajax-loader.gif" /> attributes in ASP.NET MVC
</div>
@Html.Partial("_patientList") @* this is view with patient table. Same view you will 251 How can I get the client's IP address in
ASP.NET MVC?

258 Difference between ApiController and


_patientList.cshtml:
Controller in ASP.NET MVC

@model IEnumerable<YourApp.Models.Patient> 309 What is ViewModel in MVC?

<table id="patientList" > How to return the response from an


3506
<tr> asynchronous call?
<th>
@Html.DisplayNameFor(model => model.Name)
</th> Hot Network Questions
<th>
@Html.DisplayNameFor(model => model.Number) Does proficiency in both simple and martial
</th> weapons mean my character is proficient in all
weapons?
</tr>
@foreach (var patient in Model) { Can I use the same save file for different copies of
<tr> the game?
<td>
@Html.DisplayFor(modelItem => patient.Name) How could O'Neill open a gate connection to the
Asgard
</td>
<td> Is it OK to access past the size of a structure via
@Html.DisplayFor(modelItem => patient.Number) member address, with enough space allocated?
</td>
</tr> Did Peter's mother know that Ego was not human?
} Regex question: matching lines with exactly n
</table> times the same character

Why is accuracy not the best measure for


Patient.cs assessing classification models?

public class Patient `[!]` (exclamation mark in brackets) wildcard in


{ bash
public string Name { get; set; } As a pedestrian, how to get someone to stop
public int Number{ get; set; } following you in a car?
}
Finite tiling in one dimension

PatientController.cs A vector implementation

public PartialViewResult GetPatients(string patient_Name="") Is a walnut a nut or a drupe?


{
Why do we use "an" in the phrase "an only child"?

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
var patients = yourDBcontext.Patients.Where(x=>x.Name.Contains(patient_Name)) Does a diode really follow Ohm's Law?
return PartialView("_patientList", patients);
} Is this quotient ring finite?

What does it mean by 'bringing him gasping to all


And also as TSmith said in comments, dont forget to install jQuery Unobtrusive Ajax library fours' in Harry potter?
through NuGet. Reverse count vector elements when sequence
occurs
share improve this answer edited Feb 25 '16 at 9:51 answered Jun 13 '13 at 20:49
Managing difficult employees
davioooh Alamakanambra
8,975 19 83 164 1,489 11 19 Deposting and withdrawing money from another
account to avoid ATM fee

15 For others, don't forget the jquery.unobtrusive-ajax lib needed for this. TSmith Oct 1 '13 at 20:31 Can held torches be lit by magic?

I have created a project in VS 2013, MVC5, I have Microsoft jQuery Unobtrusive Ajax, through NuGet, but it Do the wheels of taxi-capable helicopters have
power?
doesn't work, it opens in a new window :( This used to work in MVC4. Thoughts? Ctlin Rdoi Jul 30 '14
at 17:17 Why did Satoshi design Bitcoin to be mineable
only on specialized hardware, if the goal was
if you add @Html.Partial("_patientList") you get an error that this view must have IEnumerable<Patient> decentralization?
Elvin Mammadov Feb 16 '16 at 6:45
Meaning of kpsewhich
1 Best tutorial on using Ajax.BeginForm ! My only problem was misspelling the unobtrusive lib in
BundlesConfig.cs :) Thanks, @Alamakanambra! Alex Mar 1 at 18:55 Is a slow home page better than a loading bar?

add a comment

All This Work :)

28 Model

public partial class ClientMessage


{
public int IdCon { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}

Controller

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
public class TestAjaxBeginFormController : Controller{

projectNameEntities db = new projectNameEntities();

public ActionResult Index(){


return View();
}

[HttpPost]
public ActionResult GetClientMessages(ClientMessage Vm) {
var model = db.ClientMessages.Where(x => x.Name.Contains(Vm.Name));
return PartialView("_PartialView", model);
}
}

View index.cshtml

@model projectName.Models.ClientMessage
@{
Layout = null;
}

<script src="~/Scripts/jquery-1.9.1.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script>
//\\\\\\\ JS retrun message SucccessPost or FailPost
function SuccessMessage() {
alert("Succcess Post");
}
function FailMessage() {
alert("Fail Post");
}
</script>

<h1>Page Index</h1>

@using (Ajax.BeginForm("GetClientMessages", "TestAjaxBeginForm", null , new AjaxOptions


{
HttpMethod = "POST",
OnSuccess = "SuccessMessage",
OnFailure = "FailMessage" ,
UpdateTargetId = "resultTarget"
}, new { id = "MyNewNameId" })) // set new Id name for Form

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
{
@Html.AntiForgeryToken()

@Html.EditorFor(x => x.Name)


<input type="submit" value="Search" />

<div id="resultTarget"> </div>

View _PartialView.cshtml

@model IEnumerable<projectName.Models.ClientMessage >


<table>

@foreach (var item in Model) {

<tr>
<td>@Html.DisplayFor(modelItem => item.IdCon)</td>
<td>@Html.DisplayFor(modelItem => item.Name)</td>
<td>@Html.DisplayFor(modelItem => item.Email)</td>
</tr>

</table>

share improve this answer edited Mar 25 '15 at 9:30 answered Mar 25 '15 at 8:28
Likhit Suboonsunti
291 3 4

add a comment

Besides the previous post instructions, I had to install the package


Microsoft.jQuery.Unobtrusive.Ajax and add to the view the following line
23
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
share improve this answer edited Dec 6 '16 at 23:17 answered Oct 24 '13 at 3:24
Zinov Luiz
1,161 1 9 29 231 2 3

add a comment

Not the answer you're looking for? Browse other questions tagged ajax asp.net-mvc form-helpers

ajax.beginform or ask your own question.

STACK OVERFLOW

Questions
Jobs
Developer Jobs Directory
Salary Calculator
Help
Mobile

STACK OVERFLOW BUSINESS

Talent
Ads
Enterprise
Insights

COMPANY

About
Press
Work Here
Legal
Privacy Policy
Contact Us

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD