Guys I'm new to JavaScript, so not sure where the error is. Basically I've two submit buttons in my cshtml. one with the id = email and other id = sms. I've set value=" ". I'm using these buttons in a form.
what I want to do is; by clicking on these button I want to pass them a value so that I can use that value in model and controller, like in switch statement. tried various ways but still its passing null value. please advise!
function GetVal() {
var input = document.getElementsByTagName('input');
for (i = 0; i < input.length; i++) {
if (input[i].type == 'submit') {
if (input[i].id == 'email') {
//input[i].value = 'submitEmail';
//input[i].setAttribute("value", "subEmail");
document.getElementById("email").setAttribute("subEmail", "value");
break;
}
}
}
}
<input id="email" type="submit" class="@( Model.UnsubscribedEmail == null ? "unsubscribe" : "subscribe")" onclick="GetVal();" />
Model
public static void susbscription(MyModel model, string submitButton)
{
switch (submitButton)
{
case "subEmail":
//Code here
break;
}
}
Controller
[HttpPost]
public ActionResult MarketingPreferences(MyModel model, string submitButton)
{
MarketingPreferencesModel.susbscription(model, submitButton);
return View(model);
}
input[i].value = 'submitEmail'should work, are you sure its finding the right element?