I am not that good at either .net or sql.
The thing is I have a web form and a database. The form would let the user enter information in textbox and dropdownlist.
the data in the dropdownlist is saved in a table.
So, I am reading values from the table, when the users fill the form and choose from the dropdownlist the options they want, the data in the textbox and selected dropdownlist should be sent back to be saved in the database.
I have successfully read the value from the database and show them in the dropdownlist like in the following code:
public class state
{
public string stateID { get; set; }
public string stateName { get; set; }
}
[WebMethod]
public static List<state> PopulateDropDownList()
{
DataTable dt = new DataTable();
List<state> objDept = new List<state>();
SqlConnection con = new SqlConnection("Data Source = ****; Initial Catalog = LCF2016; Integrated Security = true");
{
using (SqlCommand cmd = new SqlCommand("SELECT STATE_ID, STATE_Name FROM state", con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
objDept.Add(new state
{
stateID = dt.Rows[i]["STATE_ID"].ToString(),
stateName = dt.Rows[i]["STATE_Name"].ToString()
});
}
}
return objDept;
}
}
}
<script src="
http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
//url is the path of our web method (Page name/functionname)
url: "Default.aspx/PopulateDropDownList",
data: "{}",
dataType: "json",
//called on jquery ajax call success
success: function (result) {
$('#ddlstate').empty();
$('#ddlstate').append("<option value='0'>-Select-</option>");
$.each(result.d, function (key, value) {
$("#ddlstate").append($("<option></option>").val(value.stateID).html(value.stateName));
});
},
//called on jquery ajax call failure
error: function ajaxError(result) {
alert(result.status + ' : ' + result.statusText);
}
});
});
</script>
<p>State</p>
<asp:DropDownList ID="ddlstate" runat="server" Width="160px" />
However, even if I am successfully calling the data to be shown into the dropdownlist, I am not able to insert the selected data along with the data in the textbox back into the database. In other word, the data is not saved into the database.
Here is my code for inserting the data "when click submit":
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == true)
{
Label1.Text = ("**Thanks for entering your information");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection vid = new SqlConnection("Data Source = ****; Initial Catalog = LCF2016; Integrated Security = true"))
{
vid.Open();
using (SqlCommand xp = new SqlCommand("insert into LCF2016 (Fname, Lname, Email, Birthdate, Phone, Address, City, STATE_ID, Zip, Country_ID, Days_Per_Month, Primary_Language, Secondary_Language, Occupation_ID, HearAbout_ID, Other_Skills) Values(#Fname, #Lname, #Email, #Birthdate, #Phone, #Address, #City, #STATE_ID, #Zip, #Country_ID, #Days_Per_Month, #Primary_Language, #Secondary_Language, #Occupation_ID, #HearAbout_ID #Other_Skills)", vid))
{
xp.Parameters.AddWithValue("#Fname", TextBox1.Text);
xp.Parameters.AddWithValue("#Lname", TextBox2.Text);
xp.Parameters.AddWithValue("#Email", TextBox3.Text);
xp.Parameters.AddWithValue("#Birthdate", TextBox4.Text);
xp.Parameters.AddWithValue("#Phone", TextBox5.Text);
xp.Parameters.AddWithValue("#Address", TextBox6.Text);
xp.Parameters.AddWithValue("#City", TextBox7.Text);
xp.Parameters.AddWithValue("#STATE_ID", ddlstate.SelectedValue);
xp.Parameters.AddWithValue("#Zip", TextBox8.Text);
xp.Parameters.AddWithValue("#country_ID", ddlcountry.SelectedValue);
xp.Parameters.AddWithValue("#Days_Per_Month", TextBox10.Text);
xp.Parameters.AddWithValue("#Primary_Language", ddllangp.SelectedValue);
xp.Parameters.AddWithValue("#Secondary_Language", ddllangs.SelectedValue);
xp.Parameters.AddWithValue("#Occupation_ID", ddloccup.SelectedValue);
xp.Parameters.AddWithValue("#HearAbout_ID", ddlhearabout.SelectedValue);
xp.Parameters.AddWithValue("#Other_Skills", TextBox15.Text);
xp.ExecuteNonQuery();
}
}
The error I get is
Invalid postback or callback argument. Event validation is enabled using in configuration or <%# Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
try,
by jquery
$( "#ddlstate" ).change(function() {
$('[id*=Hiddenfield1]').attr('value', $( "#ddlstate" ).val());
});
body
<asp:HiddenField ID="Hiddenfield1" runat="server">
code
xp.Parameters.AddWithValue("#STATE_ID", Hiddenfield1.vlue);
Never having tried exactly what you are doing here, my best approximation is that the ViewState is invalid when you do your PostBack because you're using Ajax to populate the drop down list. It appears you are populating your drop down list using an Ajax call, then trying to use a full page post to send that data back to the server.You have several things going on here I don't quite understand, but what I would suggest is this.
I think you need to do a server side Request / Post, i.e. bind your drop down list and text box controls server side on the request, and then use the normal postback to send the data back to the server. OR, use AJAX both ways -receive AND send your data, but not try to mix them as you are.
Related
To put it simply, I need a way for client side code to be able to trigger a server side method in my project. The way that I'm trying to use such functionality is when a user inputs their email address into a textbox, after each character is typed I want the project to trigger the method shown below which uses a class to query my database.
private void EmailCheck()
{
lblEmailError.Text = null;
Customer y = new Customer();
int counter = 0;
y.Email = Email.Text;
counter = y.CheckEmail();
if (counter.Equals(1))
{
lblEmailError.Text = "Email is already in use";
}
else
{
lblEmailError.Text = null;
}
}
I currently have almost no experience of any kind with JavaScript or any form of client side scripting. As I understand, AJAX may be of use to me here but again I am clueless about how I would implement it. I've also heard about onkeydown/press/up but again I am not sure how to alter online solutions to my specific need. Any help?
The most straightforward way would be to make a button in HTML5, use jQuery $.ajax() function to invoke a server side REST API (implementation could be anything C# Web API, Python Flask API, Node.JS API).
In your client side:
<label> Enter something into the textbox </label>
<input type = "text" id = "myTextBox" placeholder="Enter something"/>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
<script>
$(function(){
//On button click query the server
$("#myTextBox").change(function(){
var textBoxValue = $("#myTextBox).val();
var dataToBeSent = {
"data": textBoxValue
};
$.ajax(function(){
url: "http://localhost:9999/api/YourAPIName",
method: "POST",
data: JSON.stringify(dataToBeSent),
success: function(data){
console.log(data);
},
error: function(jqXHR, textStatus, errorThrown){
console.log("Failed because" + errorThrown);
}
}); //end .ajax
}); //end click
}); //end jQuery
</script>
In your Server side (Assuming C#):
Make a model class, with properties the same name as the JSON key you constructed for the [FromBody] attribute to deserialize it correctly.
public class SomeModelClass
{
public string data { get; set; }
}
[HttpPost]
[Route("api/YourAPIName")]
public HttpResponseMessage YourMethod([FromBody] SomeModelClass modelClass)
{
//perform logic and return a HTTP response
}
I have a scenario where I want to check if user adds data with same date and same region again, it should give him a prompt alert.
I tried this with code behind,like below:-
protected void btnSave_Click(object sender, EventArgs e)
{
DataTable dtExcel = new DataTable();
dtExcel.Clear();
string StrCount = String.Empty;
string connString = "";
HttpPostedFile File = FileUpload1.PostedFile;
string strFileType = Path.GetExtension(FileUpload1.FileName).ToLower();
string path = FileUpload1.PostedFile.FileName;
string Filename = path.Substring(path.LastIndexOf("\\") + 1, path.Length - path.LastIndexOf("\\") - 1);
path = Server.MapPath(#"~/Excels/" + "/" + Filename.ToString());
File.SaveAs(path);
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
string query = "SELECT * FROM [Sheet 1$]";
OleDbConnection conn = new OleDbConnection(connString);
conn.Close();
if (conn.State == ConnectionState.Closed)
conn.Open();
OleDbCommand cmd = new OleDbCommand(query, conn);
OleDbDataAdapter daExcel = new OleDbDataAdapter(cmd);
daExcel.Fill(dtExcel);
conn.Close();
DataTable DtMain = CF.ExecuteDT("select Tran_type, Order_Date, Region_Mkey from WMS_Future_Del_Order_Hdr where Tran_type = '" + CmbTypeOfAsn.SelectedValue + "' and Order_Date = convert(datetime,'" + TxtEdate.Value + "',103) and Region_Mkey = '" + ddlRegion.SelectedValue + "'"); // checks the duplicate records here
if (DtMain.Rows.Count > 0)
{
ClientScript.RegisterStartupScript(this.GetType(), "SuccessScript", "myTestFunction()", true);
}
Also see my function for prompting the alert for duplicate message
function myTestFunction() {
if (confirm('Are you sure you want to override the file ?')) {
return true;
}
else {
return false;
}
}
but what happens here is even after cancel click, the excel file is uploaded and gets saved. I dont know why
Although Abbas Kapasi's solution based on AJAX is good one (and you do not want to/can not use the solution based on CheckBox as I suggested in my other post codebhind-javascript-alert-for-yes-and-no-not-working-exactly), if you are not familiar with AJAX or don't want to use AJAX then you may achieve it using following approach. It may seem a bit odd but you may modify it according to your requirements:
I am splitting the code to upload and find existing file/record in one procedure and the code to write/overwrite the records in another sub-procedure.
Then I am using two LinkButtons, LinkButtonLoad and LinkButtonOverwrite.
LinkButtonLoad will be attached to the call the main procedure which uploads and finds existing file/record. LinkButtonOverwrite will call the procedure to write/overwrite the records. LinkButtonOverwrite will remain hidden from the user.
In the first procedure if the system finds existing file/record it will show a client side prompt to the user to overwrite or not. If the file/record does not exist it will call second sub-procedure to write the records.
On client side, if the prompt is displayed for existing file/record and the user selects Cancel/No the process will not proceed. If the user selects OK/Yes then I'll be calling the procedure linked to the LinkButton2 to overwrite the records.
Now putting it all together:
In aspx/front end
<asp:LinkButton ID="LinkButtonLoad" runat="server" Text="Load Records" CssClass="button" ... />
<asp:LinkButton ID="LinkButtonOverwrite" runat="server" Text="ow" CssClass="button hidden" ... />
I'm using hidden CSS class to hide the button .hidden {display: none;}. Do NOT use visible="false" attribute, it'll not be available on the client side.
JavaScript on client side:
function myTestFunction() {
if (confirm('Are you sure you want to override the file ?')) {
// get the action attribute of the hidden LinkButtonOverwrite here and call it on OK
// as an alternative you may also invoke the click event of this button
var defaultAction = $('#<%=LinkButtonOverwrite.ClientID %>').prop("href");
window.location.href = defaultAction;
}
else {
return false;
}
}
LinkButtonLoad in code behind:
protected void LinkButtonLoad_Click(object sender, EventArgs e)
{
//==============================================================
// YOUR CODE TO UPLOAD THE FILE, SAVE IT SO WE MAY USE IT AGAIN
//==============================================================
if (DtMain.Rows.Count > 0)
{
ClientScript.RegisterStartupScript(this.GetType(), "SuccessScript", "myTestFunction()", true);
}
else
{
MySubToOverwrite();
}
}
LinkButtonOverwrite in code behind:
protected void LinkButtonOverwrite_Click(object sender, EventArgs e)
{
//===========================================================
// AS THE FILE IS ALREADY UPLOADED IN LINKBUTTONUPLOAD_CLICK
// YOU MAY ACCESS IT AND PERFORM REQUIRED OPERATIONS
//===========================================================
MySubToOverwrite();
}
MySubToOverwrite() code in behind:
private void MySubToOverwrite()
{
//==========================================================
// OVERWRITE YOUR RECORDS HERE
// FROM THE FILE ALREADY UPLOADED IN LINKBUTTONUPLOAD_CLICK
//==========================================================
}
Detecting duplicate records
There are a few ways you can do this. Just going to touch on the big picture here, in order of my personal preference:
Easiest method: Don't check. Prompt first. Provide a checkbox up front.
Use 2 Ajax requests: one to check for existing record, and another to save the file (if user said "OK" or if file not yet exists). Challenge: multiuser environments.
Multi-step POST back. Server code injects a dynamic control (or shows a hidden static one) that prompts user to overwrite. Ugly, but does not require JS. You may remember this from the '90s.
Versioning: more advanced, more complex, but for Enterprise systems this is the way to go. Do not overwrite files with same identifier; store a different version of it. Then you get a nice history and audit trail of who changed it. Makes it easy to go back to fix mistakes, blame people, etc.
Canceling the PostBack (if using option 3 or other postback method)
You need to bind to the correct button, and to prevent POST to the server (AKA "postback") you must return false in either
the click event handler of the rendered input type="button"
or
the submit event handler if the form
You can accomplish this by embedding the return value to myTestFunction() in the asp:Button itself:
<asp:Button ID="btnSave" runat="server" Text="Save"
OnClientClick="if(!myTestFunction()){return false;}"
OnClick="btnSave_Click" />
<script>
function myTestFunction() {
return confirm('Override the file?');
}
</script>
use ajax function with webservice to check the same data and region
Submit button code
<asp:Button ID="btnupload" runat="server" OnClientClick="javascript return checkData()"
Text="Upload" OnClick="btnupload_Click"></asp:Button>
Javacript
<script>
function checkData() {
$.ajax({
type: "POST",
url: "demo.aspx/functionName1",
data: "{parameter1:'" + param1 "',parameter2:'" + param2 + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
if(msg.d == 'Yes'){
if (confirm('Are you sure you want to override the file ?')) {
return true;
}
else {
return false;
}
}
else{
return true;
}
}
});
}
</script>
C# Code
using System.Web.Services; --- use this class
[WebMethod]
public static string functionName1(string parameter1, string parameter2)
{
------------------------------;
if(check Condition)
return "Yes";
else
return "No"
}
In reality, you just need the following to make it work. Use RegisterOnSubmitStatement instead of RegisterStartupScript. It would let you add script that executes in response to the button's onclick event. The script is executed before the button event is invoked, and gives you an opportunity to cancel the event.
Code-behind:
protected void Page_Load(object sender, EventArgs e)
{
// You can place this in any method where you want to do the validation.
String scriptText =
"return confirm('Are you sure you want to override the file ?')";
ClientScript.RegisterOnSubmitStatement(this.GetType(),
"ConfirmSubmit", scriptText);
}
protected void btnSave_Click(object sender, EventArgs e)
{
}
Markup :
<asp:Button id="ConfirmSubmit" runat="server" OnClick="btnSave_Click"/>
I am trying to do this for the first time based on modifying a code snippet I found online. I am building an html data table in my code behind embedding sql data from a c# datatable. I am then trying to pass it to an .aspx page using an AJAX call and passing the html table string to a tag. The problem I am having is that when I breakpoint on the htmlTable string it looks good but the page is coming up with a javascript alert box saying 'error', not sure how to troubleshoot from this point or what is wrong with the code, I have pasted below, any assistance is appreciated!!!
Code Behind:
namespace WebApplication3
{
public partial class Page_MF : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private static string connTotalOrdersCalls = ConfigurationManager.ConnectionStrings["connTotalOrdersCalls"].ConnectionString;
public static DataTable reportDT()
{
try
{
String sp_Name = "USP_TOTAL_ORDERCALLS";
using (DataTable OrdersCalls = new DataTable())
{
OrdersCalls.Columns.Add("Week_Ending");
OrdersCalls.Columns.Add("Mth");
OrdersCalls.Columns.Add("Product_Suite");
OrdersCalls.Columns.Add("Site");
OrdersCalls.Columns.Add("SalesAction");
OrdersCalls.Columns.Add("Orders");
OrdersCalls.Columns.Add("TotalCalls");
using (SqlConnection SQLConn = new SqlConnection(connTotalOrdersCalls))
{
using(SqlCommand SQLComm = new SqlCommand(sp_Name, SQLConn))
{
SQLComm.CommandType = CommandType.StoredProcedure;
//SQLComm.Parameters.Add(new SqlParameter("#Site", null);
SQLConn.Open();
SqlDataReader sql_Reader = SQLComm.ExecuteReader();
while (sql_Reader.Read())
{
Object[] row = {
sql_Reader["Week_Ending"].ToString(),
sql_Reader["MTH"].ToString(),
sql_Reader["PRODUCT_SUITE"].ToString(),
sql_Reader["SITE"].ToString(),
sql_Reader["Orders"].ToString(),
};
OrdersCalls.Rows.Add(row);
}
}
return OrdersCalls;
}
}
}
catch (Exception)
{
throw;
}
}
[WebMethod]
public static String SendToDiv()
{
String htmlTable = "<table>";
DataTable newTable = reportDT();
for (int i = 0; i < newTable.Rows.Count; i++)
{
htmlTable += "<tr>";
for (int x = 0; x < newTable.Columns.Count; x++)
{
htmlTable += "<td>" + newTable.Rows[i][x] + "</td>";
}
htmlTable += "</tr>";
}
return htmlTable;
}
}
}
.ASPX Page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
//on document ready event (when the dom is ready)
$(document).ready(function () {
appendToDiv();
});
function appendToDiv() {
$.ajax({
type: "POST",
async: false,
contentType: "application/json; charset=utf-8",
url: "Page_MF.aspx/SendToDiv",
data: JSON.stringify({}),
dataType: "json",
success: function (data) {
$('#div-for-upload').append(data.d)
},
error: function (result) {
alert("Error");
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="div-for-upload">
</div>
</form>
</body>
</html>
according to the description you provided, below are possibilities.
1) Copy the value from htmlTable string variable. Create a HTML page and paste these content in the body section. If page displays the info, be sure, nothing wrong with the table creation.
In the provided solution, you must close the table node.
2) Check the response from developer tool like Firebug, Chrome developer tool by pressing F12 function key. It should show you the contents are being passed from server to client.
If you can see the response, your data is on the client side.
3) This condition limits us to the final option i.e you're accessing / trying to access wrong field for the data from the response.
Hope this helps.
I am currently doing an unpaid internship in C# and Asp.net. My employer has asked me to write out a javascript function so as to tell the user if they are sure if they want to delete a record from the database before deleting it.
After some research I was able to write out the Javascript function to tell the user if they are sure they want to delete this record before actually deleting it from the database.
The javascript function works. However now I have the problem of how do I call the backend C# function which will actually delete the record from the database from the front end javascript function which I have just written?
Here is my code:
Javascript function:
function watchdelete()
{
if (confirm("Are you Sure You want to delete this Manufacturer?") == true)
{
__doPostBack('btnDelete_Click','');//"PageMethods.btnDelete_Click();
}
else { }
}
Front end part which calls the javascript client side code attached to the delete button:
<asp:Button ID="btnDelete" runat="server" Text="Delete" OnClientClick=" return watchdelete()" OnClick="btnDelete_Click1" />
Back End C# function which I want to invoke in order to delete the record from the database:
(Please note I will be happy as long as I call this function and it executes, you need not worry
about its internal workings too much. Thank you)
protected void btnDelete_Click(object sender, EventArgs e)
{
String com, command, findmodel;
if (txtManufactureName.Text != "") // If the manufacturer name is not null
{
if (txtManufactureName.Text == grdManufact.SelectedRow.Cells[1].Text) // And the manufacturer name must not be
{ // Changed from selected one
string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString2"].ToString();
try
{
using (SqlConnection conn = new SqlConnection(strConnectionString))
{
conn.Open(); // Connect to database
String moderated = (checkBoxModerated.Checked) ? "true" : "false";
findmodel = "SELECT * From VehicleModels WHERE ManufacturerID = '" + txtManID.Text + "';";
com = "SELECT * From VehicleManufacturer WHERE ManufacturerName = '" + txtManufactureName.Text + "' AND Ismoderated ='" + moderated + "';";
command = "DELETE From VehicleManufacturer WHERE ManufacturerName = '" + txtManufactureName.Text + "' AND Ismoderated ='" + moderated + "';";
SqlDataAdapter finder = new SqlDataAdapter(findmodel, conn);
DataTable dat = new DataTable();
int nummods = finder.Fill(dat);
if (nummods == 0)
{
SqlDataAdapter adpt = new SqlDataAdapter(com, conn);
DataTable dt = new DataTable();
int number = adpt.Fill(dt); // try to find record to delete
if (number == 0) // If there is no such record to delete
{ // Indicate this to user with error message
txtMessage.Text = "Sorry, there is no such record to delete";
}
else
{ // Otherwise delete the record
using (SqlCommand sequelCommand = new SqlCommand(command, conn))
{
sequelCommand.ExecuteNonQuery();
txtMessage.Text = "Manufacturer Deleted Successfully";
txtManufactureName.Text = ""; // Reset manufacturer name
txtDescription.Text = ""; // Reset Description
checkBoxModerated.Checked = false; // clear moderated checkbox
}
}
}
else
{
txtMessage.Text = "Sorry. You must delete associated models first.";
}
conn.Close(); // Close the database connection. Disconnect.
}
BindGrid(); // Bind Manufacturer Grid again to redisplay new status.
}
catch (SystemException ex)
{
txtMessage.Text = string.Format("An error occurred: {0}", ex.Message);
}
}
else
{
txtMessage.Text = "Sorry. You cant change the manufacturer name before deleting";
}
}
else
{ // Otherwise give error message if manufacturer name missing
txtMessage.Text = "Please enter a manufacturer name to delete";
}
}
Any ideas will be appreciated.
Let's simplified your validation function to:
function confirmDelete(){
return confirm("Are you Sure You want to delete this Manufacturer?");
}
Then your button with OnClientClick attribute will be something like
OnClientClick=" return confirmDelete()"
As long as your validation function returns false .NET will not submit your code the server.
To give people an understanding of how to call backend functions from client side Javascript I would like to put the answer here in my own words -> Simple plain English:
Step
1. Write your Javascript function and place it on the client side and enable scripts whatever, not going into too much detail here. See the code snippet below for an example
<script type = "text/javascript" >
function watchdelete()
{
return confirm("Are you Sure You want to delete this Manufacturer?");
}
Write your button or control front end code and ensure that your OnClientClick = the name of the javascript function you want to call plus the word return in front of it as in the example asp
code shown in the original post.
Ensure you fire up the backend C# function as per usual for example by double clicking on the button or control in design view of Visual Studio 2012 or 2013 or whatever so as to automatically build its backend function in the code behind page and code your backend function for what you want it to do obviously.
When you have done step 3 correctly you should have OnClick= whatever your backend C# function was.
Test your application. Once your front end javascript returns true it should automatically fire up
your backend function as long as you have put a return statement as per the front end code shown in the original post or similar.
Life can be simple if only you want it to be. The choice is yours.
In my project I need to add functionality, that show infobox in right top corner of page, when client save something. Everything works fine when save operation do redirect to another page in my solution.
Client run save action:
[SaveAction] //my own action filter to show info box
public ActionResult Details(int id, FormCollection form)
{
var pojazd = PojazdRepo.GetById(id);;
if (UpdateAndSave(pojazd, form))
{
return RedirectToAction("Index");
}
else
{
return View(GetDetailsViewModel(id, true));
}
}
Now my action filter test that ModelState.IsValid is true then add something to TempData:
public class SaveActionAttribute : ActionFilterAttribute
{
private bool test;
private bool isAjax;
public override void OnActionExecuted(ActionExecutedContext ctx)
{
test = ctx.Controller.ViewData.ModelState.IsValid;
isAjax = ctx.HttpContext.Request.IsAjaxRequest();
base.OnActionExecuted(ctx);
}
public override void OnResultExecuting(ResultExecutingContext ctx)
{
if (test)
{
if (isAjax) ctx.Controller.TempData["ActionPopUp"] = "";
else ctx.Controller.TempData["ActionPopUp"] = "save";
}
base.OnResultExecuting(ctx);
}
}
And my Site.Master run script if TempData["ActionPopUp"] = "save":
<script type="text/javascript">
$(document).ready(function () {
var test = '<%: TempData["ActionPopUp"] %>';
if (test != '') SaveSuccessPopUp(test);
});
</script>
As mentioned, this solution works fine, when controller make Redirect and Site.Master is loaded again, my problem is, how to inject SaveSuccessPopUp() function to action result, when Action was called by AJAX and return something, what don't reload page and don't run Site.Master $(document).ready code block.
Nice question.
You need to probably work with partial view here. I mean if your request is an ajax request, append the TempData again and the TempData will be outputted inside the partial view.
How will you send that partial view output as chunk of html?
I have a blog post about how you can send the partial view as string. The topic is different but you will get the idea:
http://www.tugberkugurlu.com/archive/working-with-jquery-ajax-api-on-asp-net-mvc-3-0-power-of-json-jquery-and-asp-net-mvc-partial-views
Here is an example:
[HttpPost]
public ActionResult toogleIsDone(int itemId) {
//Getting the item according to itemId param
var model = _entities.ToDoTBs.FirstOrDefault(x => x.ToDoItemID == itemId);
//toggling the IsDone property
model.IsDone = !model.IsDone;
//Making the change on the db and saving
ObjectStateEntry osmEntry = _entities.ObjectStateManager.GetObjectStateEntry(model);
osmEntry.ChangeState(EntityState.Modified);
_entities.SaveChanges();
var updatedModel = _entities.ToDoTBs;
//returning the new template as json result
return Json(new { data = this.RenderPartialViewToString("_ToDoDBListPartial", updatedModel) });
}
RenderPartialViewToString is a controller extension. you can find the the complete code for that from below link:
https://bitbucket.org/tugberk/tugberkug.mvc/src/6cc3d3d64721/TugberkUg.MVC/Helpers/ControllerExtensions.cs
After you have your html back on the client side code, append it you DOM and work on it. Animate it, show/hide it, do whatever you need with it