vendredi 20 novembre 2015

HTML JavaScript form.submit variables

I've got a simple form that takes a selection of checkboxes and a date range. Upon clicking a button, this gets sent to my MVC controller via three variables - all strings; my issue is that one of the variables always comes back null whereas the other two work correctly.

Var 1 and 2 as such:

$("[name=start]").val(moment().subtract('month', 1).startOf('month').format('DD/MM/YYYY'));

$("[name=end]").val(moment().subtract('month', 1).endOf('month').format('DD/MM/YYYY'));

var 3 =

$("[name=trust]").val((dataList));

dataList is a string of names, seperated by "," e.g. "Craig,Ben,Tom,Harry" - ideally I wanted this to be a list but I couldn't figure out a way to pass it to the controller so I'm using this workaround.

When the code reaches the controller, var 1 and 2 arrive correctly; but var 3 is always null despite dataList always being populated with the correct data.

I can see it's because the properties of HTML variable 'trust' has .val() which contains the correct data, but when passing it to the controller it must pass the object. My confusion is that var 1 and 2 are written in exactly the same way, so I don't understand how it passes the correct property whereas var 3 passes the object.

<form action='@Url.Content("~/Report/GenSLM/")' method='post' name='slmForm'>
    <input type="hidden" name="trust" value="" />
    <input type="hidden" name="start" value="" />
    <input type="hidden" name="end" value="" />
</form>

Here is a screenshot of what I'm talking about: enter image description here As you can see in the JS watch, var start is an object which has .val() = "01/10/2015", var trust again is an object which has .val() = "data1,data2,data3". When passing them to the controller, start and end are received correctly from .val() but trusts arrives as the object - not .val().

Can anyone explain whats going wrong or where I have gone wrong? If any further details are needed please do let me know.

Aucun commentaire:

Enregistrer un commentaire