jQuery/Ajax simple form post in webmatrix

2019-09-02 16:55发布

问题:

Can someone please tell me the code I need, so that this form updates the database without needing a refresh?

@{
Layout = "~/_template1.cshtml";

var db = Database.Open("stayinflorida");
var CurrentUser = WebSecurity.CurrentUserId;
var userdetails = ("SELECT * from UserProfile WHERE UserId='8'");
var quserdetails = db.QuerySingle(userdetails, CurrentUser);

if (IsPost){
var updateuserdetails = "UPDATE UserProfile SET FirstName = @0, LastName = @1 WHERE UserID='8'";
db.Execute(updateuserdetails, Request["FirstName"], Request["LastName"]);
}
}

<h1>My Details</h1>
<hr>
<form method="post" action="~/Account/MyDetails.cshtml">
<fieldset>
<label>First Name</label>
<input class="input-xlarge" type="text" name="FirstName" placeholder=".input-xlarge" value="@quserdetails.FirstName">
<br>
<label>Last Name</label>
<input class="input-xlarge" type="text" name="LastName" placeholder=".input-xlarge" value="@quserdetails.LastName">
<button type="submit" class="btn btn-success">Update</button>
<button type="submit" class="btn btn-success">Cancel</button>
</fieldset>
</form>

What I have written kind of works, but I have to hit F5 for it to update, and it's just not what I want. I want to use jQuery/ajax, but I just don't know the code.

回答1:

You need some JavaScript if you want to use AJAX. The jQuery library simplifies this. Here's an article I wrote that features AJAX updates using WebMatrix: http://www.mikesdotnetting.com/Article/176/WebMatrix-and-jQuery-Forms-Part-2-Editing-Data