|
Thank you for your help
modified 3-Jun-17 11:56am.
|
|
|
|
|
codeproject registration page show a dropdown which show client timezone and client can change it during registration. so i like to know how codeproject registration page knows what is client timezone at their server end because we know that from server end we can not detect client timezone. only at client side we can detect client's timezone by javascript.
so please any codeproject moderator tell me how they show client timezone selected in their dropdown in registration page. just give me some idea.
|
|
|
|
|
|
thanks great
|
|
|
|
|
Hi,
I have a Web Api Get implemented in the following way
public object Get()
{
dynamic model = null;
using (AppDevSecEntities ctx = new AppDevSecEntities())
{
BSCCrystalReportsViewerEntities ctx2 = new BSCCrystalReportsViewerEntities();
string maxLanId = ctx.UserLists.OrderByDescending(x => x.LastName).First().WindowsUserName;
var tempUserJobs = (from c in ctx.UserJobs orderby c.SupervisorUserName select c).Where(x => x.WindowsUserName == maxLanId);
DataTable UserJobs = Common.ConvertToDataTable<UserJob>(tempUserJobs);
model =
new
{
SingleEmployee = ctx.spGetShortSingleEmployee(maxLanId).FirstOrDefault(),
Departments = (from c in ctx.Departments orderby c.DepartmentName select c).ToList<Department>(),
Locations = (from c in ctx.Locations orderby c.LocationName select c).ToList<Location>(),
Supervisors = (from c in ctx.Supervisors orderby c.LastName, c.FirstName select c).ToList<Supervisor>(),
Managers = (from c in ctx.Managers orderby c.LastName, c.FirstName select c).ToList<Manager>(),
Directors = (from c in ctx.Directors orderby c.LastName, c.FirstName select c).ToList<Director>(),
Applications = (from c in ctx.Applications orderby c.ApplicationName select c).ToList<Application>(),
ApplicationGroups = (from c in ctx.ApplicationGroups orderby c.ApplicationGroupName select c).ToList<ApplicationGroup>(),
ApplicationSecurityRoles = (from c in ctx.ApplicationSecurityRoles orderby c.ApplicationSecurityRoleName select c).ToList<ApplicationSecurityRole>(),<br />
UserJobs
};
}
return model;
}
I have jquery call to get this in the Employee.js file as below
loadEmployee: function (employeeID) {
$.get("/api/EmployeeAPI/Get").then(function (data) {
var list = $("#ddlApplicationGroup");
var selectedValue = list.val();
list.empty();
$.each(data.ApplicationGroups, function (index, applicationgroup) {
$("<option>").attr("value", applicationgroup.ApplicationGroupID).text(applicationgroup.ApplicationGroupName).appendTo(list);
});
if ((selectedValue == null) || (selectedValue == undefined)) {
list.prepend("<option value='-1' selected='selected'>Select value</option>");
list[0].selectedIndex = 0;
}
else {
list.val(selectedValue);
}
});
}
Now I want to bind UserJobs with a table in the cshtml, the table id is: tableDisplay, the condition is I want to loop through the rows and columns with the Column and Row index rather instead of Column Names. Can anybody please help me in this regards?
As I am not supposed to know the Column names or create this table as general table to fit for all the DataTable, I am trying to loop and read through the table and assign the value to cells.
Here I am trying but failing
var a = data.UserJobs;
var columns = a.api.columns();
a.columns().every(function () {
alert(a[0].val();<br />
});
$.each(a, function (bb) {
alert(a[bb][0]);
});
Any help is going to be very very helpful, thanks in advance.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
-- modified 30-May-17 16:38pm.
|
|
|
|
|
Hi all,
I am having trouble sending a success/fail message to a view from my controller. I have a file upload section of my website. User chooses file, presses upload button and file is saved in a folder.
My Controller code:
public ActionResult UploadFiles(int? id)
{
CI cI = db.DataBase.Find(id);
foreach (string upload in Request.Files)
{
if (Request.Files[upload].FileName != "")
{
string path = Server.MapPath(@"/App_Data/uploads/" + id + "/");
string filename = Path.GetFileName(Request.Files[upload].FileName);
Response.Write(path);
bool check = System.IO.File.Exists(Path.Combine(path, filename));
if (check==true)
{
Session["msg"] = "File with the same name already exists - Choose another file or change the file name";
System.Diagnostics.Debug.Write("file exists");
return RedirectToAction("UploadFiles");
}
if (check == false)
{
Session["msg"] = "Successfull Upload";
Request.Files[upload].SaveAs(Path.Combine(path, filename));
System.Diagnostics.Debug.Write("Upload success");
return RedirectToAction("UploadFiles");
}
}
}
return View("Upload");
}
Then I am calling the message in my view using:
@Session["msg"];
The code is working for the most part i.e. it will check if the file exists/doesn't correctly and print the correct message to the output. My problem is getting the message to the view. If I manually refresh the page my message gets to the view. So that tells me I am making a mistake with how I am redirecting.
I am quite new to Mvc so I can't identify the problem. Any help would be greatly appreciated.
|
|
|
|
|
This might be a caching issue, so when the browser requests the UploadFiles page it shows the cached version rather than asking for a new version so your message isn't there. If that's the case you can "fix" this by adding something random to the url
return RedirectToAction("UploadFiles", new { r = DateTime.Now.Ticks.ToString() });
that will add an "r" param with a pseudo-random number forcing the browser to get an updated version of the page.
|
|
|
|
|
Thanks for your answer.
Unfortunately it did not solve my issue.
The code seems to be operating as expected when using breakpoints, it is getting to the RedirectToAction line but no reload. Refresh browser and message is there!
|
|
|
|
|
Use the network tools of the browser to examine the network traffic and look at the statuses etc to see if it really is getting a cached version or not and to see if the redirect is happening etc.
|
|
|
|
|
In MVC , ViewBag , ViewData and TempDate are preferred than Session. In your case , try TempData.
|
|
|
|
|
Thanks for your reply.
I have been using all three ViewBag, TempData & Session while testing but to no avail. I agree Temp data is the best here. From my little knowledge, I have read that it is best practice to use TempData for redirects.
|
|
|
|
|
Can you show us the code for
return RedirectToAction("UploadFiles"); ?
Are you able to see the Session['msg'] in there before View.
|
|
|
|
|
RedirectToAction is a built-in method provided by the MVC framework, it's not something he has written himself.
|
|
|
|
|
I meant the
"UploadFiles" where he is redirecting to.
|
|
|
|
|
Hi everyone,
Firstly, thanks for trying to help me out with this. I have fixed the issue. The problem was not in the controller, it was in fact the view.
My problem is I am completely new to this type of coding, In fact, up to a couple of months ago, the only programming I had dabbled in was matlab for scientific analysis purposes. I probably should spend more time learning the basics, but as I have absolutely no patience, I have decided to build first and analyse later.
I would appreciate it if I could get some help understanding where I went wrong.
The view I was using was utilizing Ajax. Data was added to the form data collection and then passed to the controller via Ajax call.
The View:
@{
ViewBag.Title = "Upload";
}
<h2>Upload</h2>
<script src="~/Scripts/jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function () {
$(‘#btnUploadFile’).on(‘click’, function () {
var data = new FormData();
var files = $("#fileUpload").get(0).files;
if (files.length > 0) {
data.append("UploadedImage", files[0]);
}
var ajaxRequest = $.ajax({
type: "POST",
url: "",
contentType: false,
processData: false,
data: data
});
ajaxRequest.done(function (xhr, textStatus) {
});
});
});
</script>
<input type="file" name="FileUpload1″ id="fileUpload" /><br />
<input id="btnUploadFile" type="button" value="Upload File" />
@Html.ActionLink("Documents", "Downloads")
<p>
@TempData["msg"]
</p>
I had a feeling (Uneducated guess!)that using the Ajax request was the problem so I changed my approach. I used the Html.BeginForm() extension method
I changed my view to:
@using (Html.BeginForm("UploadFiles", "CIsAdmin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="FileUpload1" /><br />
<input type="submit" name="Submit" id="Submit" value="Upload" />
}
@Html.ActionLink("Documents", "Downloads")
<p>
@TempData["msg"]
</p>
Can someone explain in simple terms, what the issue was?
FYI, this is my controller code:
public ActionResult UploadFiles(int? id,string msg)
{
CI cI = db.LotusCIs.Find(id);
foreach (string upload in Request.Files)
{
if (Request.Files[upload].FileName != "")
{
string path = Server.MapPath(@"/App_Data/uploads/" + id + "/");
string filename = Path.GetFileName(Request.Files[upload].FileName);
Response.Write(path);
bool check = System.IO.File.Exists(Path.Combine(path, filename));
if (check==true)
{
TempData["msg"] = "File with the same name already exists - Choose another file or change the file name";
return RedirectToAction("UploadFiles", "CIsAdmin") ;
}
if (check == false)
{
TempData["msg"] = "Successfull Upload";
Request.Files[upload].SaveAs(Path.Combine(path, filename));
return RedirectToAction("UploadFiles", "CIsAdmin");
}
}
}
return View("Upload");
}
Thanks in Advance
|
|
|
|
|
The difference between the Ajax and Submitting the form is that in Ajax , it is Async call and it does not reload your view. The ajax call receives the response from your action (URL) and does the update with the controls in the view based on your code.
Whereas in form submission , the action is called with post data and the view is reloaded again with action response.
So you have to check how you update the response message in your view based on these two different behaviours.
Hope this helps
|
|
|
|
|
Hi,
I got considerable gap in using front end tools like html or jquery etc, I am able to add options to my select by using jquery, what I want is, for each option I want to show remove button or image beside it, so that when user clicks on that image it will remove that option or delete that option. I tried to add using span or button or image but nothing worked so far, any help is going to be much helpful.
Thanks in advance my friends.
The way I am adding the options to select list is as below
$("#btnAddApplicationGroup").click(function (event) {
var $ddl = $("#ddlApplicationGroup"); //Getting the id of the dropdown
var $list = $("#lbxApplicationGroup"); // Getting the id of the listbox
var selValue = $ddl.val();
var selText = $ddl.find("option:selected").text();
if (($("#lbxApplicationGroup option[value='" + selValue + "']").length <= 0) && (selValue >= 1)) {
var $opt = $("<option></option>");
$opt.html(selText);
$opt.attr("value", selValue);
$list.append($opt);
}
});
$("#btnAddReportPack").click(function (event) {
var $ddl = $("#ddlReportPack"); //Getting the id of the dropdown
var $list = $("#lbxReportPack"); // Getting the id of the listbox
var selValue = $ddl.val();
var selText = $ddl.find("option:selected").text();
if (($("#lbxReportPack option[value='" + selValue + "']").length <= 0) && (selValue >= 1)) {
var $opt = $("<option></option>");
$opt.html(selText);
$opt.attr("value", selValue);
$list.append($opt);
}
});
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
Hi,
I am using Web Api call, jQuery in my MVC application View to populate fields which is of Customers,
- I am planning to implement a partial View which has a table to display Purchase History of selected Customer, when a new item is selected in the View fields, then the partial View should display the values in the Table, but I don't know how to render partial view when using Web Api
- Another one is same this partial view contains one dropdown list, list box, add and delete buttons, when we select an item from dropdown list and say add then it will add in the listbox, when we select from listbox and say delete it will delete from the listbox. But by default when a Customer is selected it will display the selected items of this purchase in the Listbox.
Can anybody please suggest me little bit about Partial Views?
This is my Web Api Get, for first try I am calling the max Last Name Customer values to test:
public object Get()
{<br />
dynamic model = null;
using (AppDevSecEntities ctx = new AppDevSecEntities())
{
BSCCrystalReportsViewerEntities ctx2 = new BSCCrystalReportsViewerEntities();
string maxCustId = ctx.Customers.OrderByDescending(x => x.LastName).First().CustId;
DataTable Orders = Common.ConvertToDataTable<Order>(ctx.Orders.Where(x=> x.CustId==maxCustId));
DataTable SelectedProducts = Common.ConvertToDataTable<Product>(ctx.Products.Where(x=> x.CustId==maxCustId));
//return
model=
new
{
SingleCustomer = ctx.spGetShortSingleCustomer(maxCustId).FirstOrDefault(),
Orders = Common.ConvertToDataTable<Order>(ctx.Orders.Where(x=> x.CustId==maxCustId)), //This is DataTable type
SelectedProducts = Common.ConvertToDataTable<Product>(ctx.Products.Where(x=> x.CustId==maxCustId)) //This is DataTable type
};
}
return model;
}
Here is how I am calling the get, in this call only the Partial View should display me the table with selected Orders and another Partial View should display me the List box with selected items, those values are being returned by the Get call. Any help will be lot helpful - thanks in advance friends.
public object Get()
{<br />
dynamic model = null;
using (AppDevSecEntities ctx = new AppDevSecEntities())
{
BSCCrystalReportsViewerEntities ctx2 = new BSCCrystalReportsViewerEntities();
string maxCustId = ctx.Customers.OrderByDescending(x => x.LastName).First().CustId;
DataTable Orders = Common.ConvertToDataTable<Order>(ctx.Orders.Where(x=> x.CustId==maxCustId));
DataTable SelectedProducts = Common.ConvertToDataTable<Product>(ctx.Products.Where(x=> x.CustId==maxCustId));
//return
model=
new
{
SingleCustomer = ctx.spGetShortSingleCustomer(maxCustId).FirstOrDefault(),
Orders = Common.ConvertToDataTable<Order>(ctx.Orders.Where(x=> x.CustId==maxCustId)), //This is DataTable type
SelectedProducts = Common.ConvertToDataTable<Product>(ctx.Products.Where(x=> x.CustId==maxCustId)) //This is DataTable type
};
}
return model;
}
$.get("/api/EmployeeAPI/Get").then(function (data) {
var list = $("#ddlProduct");
var selectedValue = list.val();
list.empty();
$.each(data.Products, function (index, Product) {
$("<option>").attr("value", Product.ProductID).text(Product.ProductName).appendTo(list);
});
if ((selectedValue == null) || (selectedValue == undefined)) {
list.prepend("<option value='-1' selected='selected'>Select value</option>");
list[0].selectedIndex = 0;
}
else {
list.val(selectedValue);
}
var date;
if ((data.SingleCustomer.PurchaseDate != null) && (data.SingleCustomer.PurchaseDate != '') && (data.SingleCustomer.PurchaseDate != 'undefined')) {
date = data.SingleCustomer.PurchaseDate;
var d = new Date(date.split("/").reverse().join("-"));
var dd = d.getDate();
var mm = d.getMonth() + 1;
if (dd < 10) { dd = '0' + dd }
if (mm < 10) { mm = '0' + mm }
var yy = d.getFullYear() + "";
var Purchasedday = yy + "-" + mm + "-" + dd;
$("#dtFieldPurchaseDate").val(Purchasedday);
}
if ((data.SingleCustomer.RePurchaseDate != null) && (data.SingleCustomer.RePurchaseDate != '') && (data.SingleCustomer.RePurchaseDate != 'undefined')) {
date = data.SingleCustomer.RePurchaseDate;
var d = new Date(date.split("/").reverse().join("-"));
var dd = d.getDate();
var mm = d.getMonth() + 1;
if (dd < 10) { dd = '0' + dd }
if (mm < 10) { mm = '0' + mm }
var yy = d.getFullYear() + "";
var Purchasedday = yy + "-" + mm + "-" + dd;
$("#dtFieldRePurchaseDate").val(Purchasedday);
}
});
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
-- modified 23-May-17 20:03pm.
|
|
|
|
|
You do it in the View using Razor if that's what your using
@Html.Partial("Name of the partial view in the Views root")
You can place your JQuery file in the "Section Scripts" in the partial view if you have one so that it loads or runs.
If it ain't broke don't fix it
|
|
|
|
|
Thank you very much and for bearing me my friend, but there is one problem I am thinking how can I solve in this,
- as I am making a jQuery call to Web api to load the data into Parent View, the same jquery method is also embedding the Datatable which is going to be bound with the fields in the Partial View. So I want to use this same call from the same parent (means Customer.js) file only I want to bind the fields of the Partial View. Means in the jquery function where I am binding the values of parent page I want to bind the fields of the partial view as well.
- the second question I have is like for example if I am using a table in the partial view I want to change its id depending upon the jquery get object name, for example if I am rending the same partial view twice, and the Web Api returns two different Datatable objects, I will have that information at the jquery call that which object I am assigning to, but I want to change the ids of the fields of the partial view depending upon the DataTable I am binding.
Is there any way I can do these two my friend? If my question is not properly put please let me know my friend? Thanks a lot.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
-- modified 24-May-17 19:39pm.
|
|
|
|
|
I understand what you want to do, I have just never done it. I never thought about using a partial view for data presentation or work form.
I think some critical thinking is required here.
Remember that the Controller is called first to populate a model, then add some ViewData if required.
Then the View is rendered, including your partial view.
Now the HTML is downloaded by the browser and begins to render.
Once the DOM is set in the browser, Ready is called and your JQuery then runs.
Technically, as an example in the Microsoft Owin Security package, I think ViewData is passed from the parent View to the Partial View. This means you should be able to pack a model into ViewData that will be passed along to the partial. I've done it with Sessions, in which I loaded a model into a session for checkout, and I've done it with ViewData as well.
so it you have a ViewData, this would pull the model back out of the ViewData in the partial view.
var names = (model_array_names)ViewData["NameArray"];
And to pack the ViewData["NameArray"] in the controller
ViewData["NameArray"] = model_array_names;
I would just design a better model or form layout first. If you have to make a choice on a form before data is loaded, then I would design HTML containers for all the scenarios, and then fill those containers with an AJAX call via JQuery.
If it ain't broke don't fix it
|
|
|
|
|
how can we get cell_double click event to be fired on gridview in asp.net
|
|
|
|
|
|
Hi all,
I am developing an application that allows users to upload/download/delete files. I have it working but I am struggling to add the following function.
Check if the file already exists in the specified folder,
if the file exists already, return a message telling the user to choose another name.
if the file doesn't exist, save the file in the specified folder.
This is my controller code:
public ActionResult UploadFiles(int? id)
{
CI cI = db.Database.Find(id);
foreach (string upload in Request.Files)
{
if (Request.Files[upload].FileName != "")
{
string path = Server.MapPath("~/App_Data/uploads/" + id + "/");
string filename = Path.GetFileName(Request.Files[upload].FileName);
Response.Write(path);
Request.Files[upload].SaveAs(Path.Combine(path, filename));
}
}
return View("Upload");
}
The upload view:
@{
ViewBag.Title = "Upload";
}
<h2>Upload</h2>
<script src="~/Scripts/jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function () {
$('#btnUploadFile').on('click', function () {
var data = new FormData();
var files = $("#fileUpload").get(0).files;
if (files.length > 0) {
data.append("UploadedImage", files[0]);
}
var ajaxRequest = $.ajax({
type: "POST",
url: "",
contentType: false,
processData: false,
data: data
});
ajaxRequest.done(function (xhr, textStatus) {
@ViewBag.Message
});
});
});
</script>
<input type="file" name="FileUpload1" id="fileUpload" /><br />
<input id="btnUploadFile" type="button" value="Upload File" />
@Html.ActionLink("Documents", "Downloads")
|
|
|
|
|
When you have the target file name ("Path.Combine(path, filename)") use File.Exists to check if it exists
File.Exists Method (String) (System.IO)[^]
If it does exists you can return Json from your action, something like {success:false}, and in your ajax call you can examine the result of the call and if it is a json object where success==false then you know the upload didn't work so you can show a message, otherwise do what you normally do with the result.
You'll maybe want to run through all the files first to check for existing files and return the failure message if any exist and if not run through them again to save them.
|
|
|
|
|