So I are getting closer. (Why is my answer in green this is not solved)
When I change my dropdown menu it refreshes the whole page. Which means we end up back at the beginning. ie first member .
What I think should happen (want to happen) is when the dropdown is changed the MemberForm model is loaded with new variables and the boxes change without refreshing the screen as that is unnecessary wast of time to the end user.
Once I have this working the I also need to be able to test that data is saved to database else flag a question to save or continue and disregard changes. This may change how this onchange is achieved.
How do I pass the int value from the (database Key) dropdown to the PopulateFields routine.
FYI calling MemberMaintanance() routine has same affect.
To make the form submit without refreshing the page you are going to have to use AJAX (ie. client-side JavaScript that calls server functions). This is too big a subject to really cover here and if I where you I'd work on getting the form to work without AJAX first - a page reload isn't necessarily a bad thing as it allows you to update state, display messages etc.
If you really want to get into AJAX then maybe check out this:
Ok so I have done some reasearch on JQuery and server side acceess.
And have got this.
asmx.cs server file
using System.Web.Services;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace IBDMemberManager
{
/// <summary>
/// Summary description for MemberService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class MemberService : System.Web.Services.WebService
{
[WebMethod]
public MemberDetails GetMemberByID(int MemId)
{
MemberDetails Member = new MemberDetails();
string cs = ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spGetMemberById", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@Id";
parameter.Value = MemId;
cmd.Parameters.Add(parameter);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Member.FName = rdr["FName"].ToString();
Member.LName = rdr["LName"].ToString();
Member.Position = rdr["Position"].ToString();
}
}
return Member;
}
}
}
Dropdown list on change
I have a dropdown on a partial view for a page. Its the only thing on the view.
I have a series of other text boxes on a second partial view on the page.
When the drop down is changed I want to load data into the text boxes form a database table.
I can get the data from the database fine.
What I need know is how do I initialize the onchange event and call a routine from my surface controller.
The on change routine will need to pass a id for the record in the table.
Can someone please point me in the right direction.
Hi Daniel
You need angular or jQuery to make logic like that on the client side, depends on your experience and preferences.
Alex
Ensure your dropdown is within a form that POSTS to your surface controller. Then the simplest way is to add this:
This will use vanilla JS to submit the form when the dropdown changes, passing any form values to the controller.
Sorry dont understand
new to this
Searching around and trying differnet things I have this
I have a
@Html.DropDownListFor(x => MemberPicker.PickedItem, MemberPicker.PickerItems, new { @id = "MemberSelector", @class = "ControlList", @onchange= "/MySurfaceController/PopulateFields(1500)" })
1500 is a interger value that should change from the drop down menu
Generated from above
futher down page I call PartialView with the form layout of infomation selected from value above dropdown
@Html.Action("MemberMaintenance", "IBDMembershipFormSurface", new { NodeID = Model.Id, GlobalSettingsId = globalSettingsId })
in my surface controller I have
I know the populate routine works and loads data as if I call it from the MemberMaintenance routine it loads all the fields.
I just cant get the dropdown list to call it.
You can't call server side methods from an HTML control. So your onchange event will never work - it has to call a client-side JavaScript method.
So the below will never work:
As mentioned, you need to trigger the form to submit to send the values from the form to your surface controller server side, so use:
Ensure you have wrapped your controls in a FORM using something like:
See https://our.umbraco.com/Documentation/Reference/Templating/Mvc/Forms/tutorial-partial-views
All this does is refreshes the screen back to the start. Doesn't call the routine PopulateFields. Doesn't keep the selected value in the dropdown box.
So I are getting closer. (Why is my answer in green this is not solved)
When I change my dropdown menu it refreshes the whole page. Which means we end up back at the beginning. ie first member .
What I think should happen (want to happen) is when the dropdown is changed the MemberForm model is loaded with new variables and the boxes change without refreshing the screen as that is unnecessary wast of time to the end user.
Once I have this working the I also need to be able to test that data is saved to database else flag a question to save or continue and disregard changes. This may change how this onchange is achieved.
How do I pass the int value from the (database Key) dropdown to the PopulateFields routine.
FYI calling MemberMaintanance() routine has same affect.
Partial View for page content
@inherits UmbracoViewPage
@using USN.USNTemplate; @using IBD.IBDModels; @using IBD.IBDHelper; @using IBD.IBDControllers;
@using USNOptions = USNStarterKit.USNEnums.Options;
@using (Html.BeginUmbracoForm
......... Does some other stuff ..............
......... Does some other stuff ..............
// This section displays data related to member selected above.
}
I are calling this routine as I only thought it would only load the data related to the model rather than the whole partial view.
The model holds the variables for the text boxes in the patial view.
PopulateFields(int MemKey) Controller routine has changed from above
To make the form submit without refreshing the page you are going to have to use AJAX (ie. client-side JavaScript that calls server functions). This is too big a subject to really cover here and if I where you I'd work on getting the form to work without AJAX first - a page reload isn't necessarily a bad thing as it allows you to update state, display messages etc.
If you really want to get into AJAX then maybe check out this:
https://codeshare.co.uk/blog/how-to-change-a-form-in-mvc-to-submit-with-ajax/
Ok so I have done some reasearch on JQuery and server side acceess.
And have got this.
asmx.cs server file
script
my drop down
But I get a Localhost says [object Object]
From what I can make out It must be something to do with this line var jqueryXml = $(data); but Im not sure what I have wrong.
Got this work except I need to pass some extra variables to the partial view
j script looks like this.
GlobalSettingsID abd NodeID are the extra 2 variables if I remove these the form reloads except I need to remove some other code in the function.
These variables are declared
Model.Id is the ID of the form model
and
Any Surgestions.
The HTML code further down the page is
solved
is working on a reply...