Pass combobox value from view to controller using #Html.ActionLink - asp.net

I have a kendo.combobox() control on my Layout.cshtml page which is a list of projects.
#(Html.Kendo().ComboBox()
.Name("projects")
.Placeholder("select project")
.DataTextField("Text")
.DataValueField("ID")
.HtmlAttributes(new { style = "width:45%;" })
.Filter("contains")
.SelectedIndex((int)Session["ProjectNoIndex"])
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetProjectsList", "Home");
})
.ServerFiltering(true);
})
)
I have a menu as well in the Layout.cshtml which is created using #Html.ActionLinks (one sample below).
<li>#Html.ActionLink("Documents", "Search", "Document", new { projectNo = 1}, new {#id = "send"})</li>
Search action in Document controller and most other actions in the application expect the projectNo parameter (entire application works based on the selected project of the user). So, I thought of saving the selected ProjectNo in Session but if I do this then users can only work with 1 project even if they open the application in multiple tabs/windows in a single browser (sessions are shared across tabs/windows).
So, I thought of passing the selected ProjectNo using querystrings. I tried below, which worked fine except when user right clicks and open's the link in new tab. in such case, below click event does not fire.
$('#send').click(function () {
var x = $("#projects").data("kendoComboBox").selectedIndex;
var url = '#Url.Action("Search", "Document", new { projectNoIndex = "-1" })'
url = url.replace("-1", x);
this.href = url;
});
I don't want to restrict user by oncontextmenu = "return false;" option either. What are my options here?
Q1: How can I make sure I can pass the selected ProjectNo using querystring
Q2: Is there any other alternative to Session where I can persist the project information across tabs/windows which can be easily available in the entire application per user, per tab/window.

Related

Xamarin Form passing parameters between page keep a hard link to it

I try the new Shell of Xamarin Form 4 for a small project.
I have a list of order, then someone chooses an order and start picking some inventory for this order with barcode. To be simple, I use 2 views and 2 viewmodel.
The process is:
1. User select an order the click a button "pickup"
2. I use ViewModelLocator (TinyIoc) to resolve the correct ViewModel of the pickup view
3. I call Initialize on the new ViewModel and pass some parameters needed. Like a list of items needed to be pickup.
4. Open the view in modal state.
I don't understand that if I change some qty in the list I pass on the second viewmodel, then hit the back button (modal view close). The quantity changed is now in the original viewmodel. How this is possible? I was thinking that passing parameter to a function do not share the same variable but just copy it??
Viewmodel of the first view (look at the Initialize function the List passed and the JobEnCours object)
private async Task GoPickup()
{
Device.BeginInvokeOnMainThread(async () =>
{
if (this.CodeJob != null && this.CodeJob != "")
{
this.IsBusy = true;
PrepChariotSP3ViewModel vm = ViewModelLocator.Resolve<PrepChariotSP3ViewModel>();
await vm.InitializeAsync(this._jobEnCours, this.ComposantesPick.ToList()) ;
await Shell.Current.Navigation.PushAsync(new PrepChariotSP3Page(vm));
this.IsBusy = false;
}
});
}
the the Initialize code on the second Viewmodel (look I set the JobEnCours.Description=Test)
public async Task InitialiseAsync(Job jobEnCours, List<ComposantePick> composantePick)
{
Title = "Ceuillette: " + jobEnCours.CodeProd;
this._jobEnCours = jobEnCours;
this.ComposantesPick = new ItemsChangeObservableCollection<ComposantePick>();
foreach(ComposantePick c in composantePick)
{
this.ComposantesPick.Add(c);
}
jobEnCours.Description = "test";
So, If I do the back button, then in the first VM the JobEnCours.Description is now set to "test" how this is possible?!
Any idea?

ASP.NET Simulate Postback in Windows Application

I need to access information on a url which loads after filling its form and clicking on a button (postback). but I don't want to load page and fill the form and click that button programmatically, Is there a way to load a url with a header (or any other way ) containing those information (filled form and clicked on button) ?
Windows form or ASP.NET way solutions ...
You can use a WebClient for that. It can send a NameValueCollection of form fields and their values as a POST to an external url.
using (WebClient client = new WebClient())
{
byte[] response = client.UploadValues("http://www.post_url.nl", new NameValueCollection()
{
{ "form_input_name_1", "value_1" },
{ "form_input_name_2", "value_2" },
});
string responseString = Encoding.Default.GetString(response);
}

ASP.Net Drop Down List not passing a value when updated using ajax

I have some jQuery that I'm using to open a pop-up window where a new consignor can be added to the database. The original window has a dropdownlist of all of the current consignors. When you add the new consignor in the pop-up window, that window closes and the original window then reloads the dropdownlist's data and selects the one just created.
All of that works perfectly. My issue is that when you fill out the rest of the form and submit it, it passes an empty string instead of the value of the selected item. Is this because it's an ASP.Net script? I don't know a lot about ASP.Net, but I've never had this issue with PHP. Can someone explain how I would go about refreshing the dropdownlist without refreshing the entire page and still get the list to pass it's value upon form submission?
My javascript code on the page that opens the pop-up and reloads the list is below:
function openConsignorAdd() {
var url;
url = "/admin/consignor/csAdd.aspx";
window.open(url, "WizardWindow", "width=400,height=500,resizable=yes,scrollbars=yes");
}
function loadNewAdded(fn, cs_txt_id) {
// var pagePath = window.location.pathname;
var pagePath = "/admin/getNewList.asp";
var paramList = "data=";
//Call the page method
$.ajax({
type: "POST",
url: pagePath + "?type=" + fn + "&cs_txt_id=" + cs_txt_id,
data: paramList,
success: function (data) {
//create jquery object from the response html
var $response = $(data);
//query the jq object for the values
var results = $response.filter('select#results').html();
if (fn == "consignor") {
$("select#<%=itemConsigner.ClientID%>").html(results);
} else if (fn == "cdr") {
$("select#<%=itemCDR.ClientID%>").html(results);
}
},
error: function () {
alert("Failed To Refresh!\n\nYou must manually refresh the page.");
}
});
}
My javascript code on the pop-up page to refresh the list is:
function refreshOpener(cs_txt_id) {
window.opener.loadNewAdded("consignor", cs_txt_id);
}
Those both work. And to get the value of my dropdownlist, I simply use:
if (itemConsigner.SelectedValue.ToString() != string.Empty)
{
itemCsTxtId = itemConsigner.SelectedValue.ToString();
}
with my dropdownlist being:
<asp:DropDownList ID="itemConsigner" runat="server" TabIndex="1"></asp:DropDownList>
If you need more info, just let me know. Any help is appreciated.
It seems that the issue is that since I am making the change after the page loads, the server does not see my new addition as one of the original options so ignores it completely. This is good so that people cannot just edit your forms I guess. So what I did was instead of getting the value of itemConsigner.SelectedValue, I grab the value for Request.Form["itemConsigner"] with the long ID. That way it doesn't validate that my submitted option was an original option.
Might be a silly observation but without all the code I'm not sure if this is the case. Are you just updating the original list with the id in the select options. The value needs to be populated as well for each. That could be why you are getting an empty value on after form submission.

ASP.Net MVC 3 Controller Action and Open New Window

i have a controller and an action. this action is to save data into database. and now, i want when i submit a button, my controller do an action and open new window.
public ActionResult New(FormCollection collection)
{
data.Population_Code = collection["Countrys[0].CountryCode"];
data.Population_Desc = collection["Countrys[0].CountryDesc"];
data.Population_Grouping = collection["Countrys[0].CountryGroup"];
data.Population_Type = "CNTRY";
data.Population_Redudant = "N";
data.Population_Modified_At = officeCode.User_Office.ToString();
db.SaveChanges();
//example for new window
//window.open('/Report/New.aspx')
return RedirectToAction("index");
}
so my controller do an action and open a new window.
anyone can help me ?
thanks
Technically, this can be done by returning javascript that will open the new window.
However, most browsers will kill a new window called in this manner (i.e. popup blocker).
You would be better off, if possible, by opening the link to your action in a new window from the start;
#Html.ActionLink("New report", "New", "Report", null, new {target = "_blank"})
Edit
I can see from your action, that it is probably a form that creates the report; you can also use the attribute target='_blank' on a form as well.

Can inserts on ASP .net Dynamic Data be done on the same page with the list view?

I would like to have users insert and edit information about entities on the same page in a similar fashion to google alerts: http://www.google.com/alerts/manage
Any advice on how this could be achieved?
It is possible changing the routing in the method RegisterRoutes in Global.asax as indicated in the remarks section present in the same file.
Enabling this section:
routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {
Action = PageAction.List,
ViewName = "ListDetails",
Model = DefaultModel
});
instead of the already enabled:
routes.Add(new DynamicDataRoute("{table}/{action}.aspx")
{
Constraints = new RouteValueDictionary(new { action = List|Details|Edit|Insert" }), Model = DefaultModel
});
It's also possible to enable custom routes for particular tables.

Resources