Shorthand if/else statement inside Ajax

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Shorthand if/else statement inside Ajax



In my razor View i have this if/else statement (Its check if there is no Serial number then print Not available otherwise if there is any showing it in Dropdown List):


@
var SerialNos = String.Join(", ", Model.SerialNo);


@if (SerialNos == "")

<div class="col-md-4">
<div class="form-group">
<label>Serial Number</label>
<input name="not available" id="notavailable" class="form-control" value="Not available">
</div>
</div>


else

<div class="col-md-4">
<div class="form-group">
<label>Serial Number</label>
<select class="form-control" id="ddlSerial">
<option value="@SerialNos">@SerialNos</option>
</select>
</div>

</div>




And i did if/else in my Script like this , but is there anything i can do to make it shorter in my JavaScript?

Can anyone please help me or point me in the right direction!

Thanks in advance :)



JavaScript:


<script>

$(document).ready(function ()

var model =

serialnumber: $("#notavailable").val(),
SelectedSerieText: $("#ddlSerial option:selected").text(),



if (model.serialnumber === 'Not available')

$.ajax(
type: 'POST',
url: '@Url.Action("ProcessRequestRMA", "Account")',
dataType: 'json',
data:
Serienummer: model.serialnumber

,
success: function (status)

if (status)

status.Serienummer = model.serialnumber;

console.log("Send");


else
alert("Something Wrong");


,

error: function ()
console.log('something went wrong - debug it!');

);



else


$.ajax(
type: 'POST',
url: '@Url.Action("ProcessRequestRMA", "Account")',
dataType: 'json',
data:

Serienummer: model.SelectedSerieText,


,
success: function (status)

if (status)

status.Serienummer = model.SelectedSerieText;


console.log("Send");


else
alert("Something Wrong");


,

error: function ()
console.log('something went wrong - debug it!');

);





);

</script>





Why are you creating an <input ../> for 'not available'? And why are you creating a <select> with only one option? (and there is no reason to repeat all the <div> and <label> elements
– Stephen Muecke
Aug 8 at 12:00


<input ../>


<select>


<div>


<label>





Messy question IMO. Do you want your if/elses to be shorter in c# or javascript? But from what i gathered, have a look at: docs.microsoft.com/en-us/dotnet/csharp/language-reference/…
– Joel
Aug 8 at 12:01






@Joel sorry ma bad , i want for javaScript :)
– 7 seconds
Aug 8 at 12:06





@StephenMuecke thanks for pointing in the right direction :) i just changed ma code, but there is i can do to make shorter in ma Javascript ! :)
– 7 seconds
Aug 8 at 12:08





You can reduce all this down to a few lines - both the html and javascript :) But you have not answered the question in my first comment (your html makes no sense)
– Stephen Muecke
Aug 8 at 12:10










By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered