I want to set default value that is missing when click on the dropdown list.I would like to be unable to select "Please select" value

how to set default value in dropdownlist in html
how to set default value in dropdown using javascript
use the `defaultvalue` or `value` props on <select> instead of setting `selected` on <option>.
set selected value of dropdown in javascript
how to set default value in dynamic dropdownlist in html
html select default value
select default value react
how to set default value in select option in jquery

I want to set default value which will miss when click on the dropdown list.I would like to be unable to select "Please select" value. When I click on "Please select" value in materialId or depotId, "" null value send by ajax and I am getting error. How can I prevent this?

Create.cshtml

  <div class="form-group">
        @Html.LabelFor(model => model.materialId, "Material names", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("materialId", null, "Please select", htmlAttributes: new { @class = "form-control chosen" })
            @Html.ValidationMessageFor(model => model.materialId, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.depotId, "Product Outlet", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("depotId", null, "Please select", htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.depotId, "", new { @class = "text-danger" })
        </div>
    </div>
    <script type="text/javascript">
            $(document).ready(function () {
                $('#depotId').change(function () { sendDataByAjax(); });
                $('#materialId').change(function () { sendDataByAjax(); });
        })
    function sendDataByAjax() {
        var materialId= $('#materialId option:selected').val();
        var depotId= $('#depotId option:selected').val();

        if (materialId == "" || depotId == "") {
          // I can not write this
        }
        $.ajax({
            type: "GET",
            url: "@Url.Action("GetStock", "OutgoingProduct")",
            data: {
                'materialId': materialId,
                'depotId': depotId
            },
            success: function (data) {
                $("#stock").html(data);
                }
        });
        }
    </script>
}

I am getting error here when "" goes to my controller. Because it is not int.

OutgoingProductController.cs

public string GetStock(string materialId, string depotId)
{
    int did = int.Parse(depotId);
    int mid = int.Parse(materialId);

Since your dropdownlist selected values are passed as numeric values, you may use parseInt() function to try parsing numeric value from client-side and then check against NaN, if the value is numeric then trigger AJAX callback:

function sendDataByAjax() {
    var materialId = parseInt($('#materialId option:selected').val());
    var depotId = parseInt($('#depotId option:selected').val());

    if (isNaN(materialId) || isNaN(depotId)) {
        // do something, e.g. alert user
        return false;
    }
    else {
        $.ajax({
            type: "GET",
            url: "@Url.Action("GetStock", "OutgoingProduct")",
            data: {
                'materialId': materialId,
                'depotId': depotId
            },
            success: function (data) {
                $("#stock").html(data);
            }
        });
    }
}

Then make sure that your action method contains int type for both parameters, hence no need to use int.Parse() which will throwing exception if parsed string has null value:

public ActionResult GetStock(int materialId, int depotId)
{
    int did = depotId;
    int mid = materialId;

    // other stuff
}

How can I set the default value for an HTML <select> element , Set selected="selected" for the option you want to be the default. <option but not considered a valid value (something like "complete here", "select your nation" ecc.) inside the option tag, which you want to appear by default in your drop down list. value attribute of tag is missing, so it doesn't show as u desired selected. I typically use a formula to get a default value to appear in a cell, even if the cell also has a DV list to allow quick access to other options. Just turn off the ERROR ALERTS in your DV settings long enough to enter a formula, something like this to watch for column A to start a row of data, so if A2 had a value, this formula in B2 or C2 or


Try returning false when materialId == "" || depotId == "" :

function sendDataByAjax() {
        var materialId= $('#materialId option:selected').val();
        var depotId= $('#depotId option:selected').val();

        if (materialId == "" || depotId == "") {
          return false;
        }
        $.ajax({
            type: "GET",
            url: "@Url.Action("GetStock", "OutgoingProduct")",
            data: {
                'materialId': materialId,
                'depotId': depotId
            },
            success: function (data) {
                $("#stock").html(data);
                }
        });
}

Javascript set select option value to null, You want to check if it's an empty string instead: I am using this piece of code to set the field's The SQL concept of null is different than null in programming languages like JavaScript or Scala. You aren't able to set a lookup field directly to this result. The DropDownList with Default Selected value will be populated from� I want to set default value which will miss when click on the dropdown list.I would like to be unable to select "Please select" value. When I click on "Please select" value in materialId or depotId


the way to solve this is not the way you're trying to do it. the way to do should be to use the validation system, available for MVC.

Change your get method to use a model, something like:

public class StockRequestModel
{
    [Required]    
    public int materialId { get; set }

    [Required]
    public int depoId { get;set; }
}

Your controller method can then become something like:

public string GetStock([FromUri] StockRequestModel model)
{
    if ( !ModelState.IsValid )
    {
         some code here
    } 

    //at this point your model is valid and you have IDs so can proceed with your code
}

Normally, in MVC you would return the original View with the state in the result, so you can then trigger the front end validation. In your case, you seem to have a WebAPI controller in an MVC app, but you can still use front end validation.

There are other questions related to yours, such as Client side validation with Angularjs and Server side validation with Asp.net MVC

Normally I'd vote to close it as duplicate, but in this case, I think it's worthwhile pointing out the issue and solution.

Another place to go would be https://angularjs.org/ and then check the Form Validation section for pure front end validation. Of course you'd want to keep both front end and back end validations.

Excel Data Validation Tips and Troubleshooting, When you try to create an Excel data validation dropdown list, and refer To download the sample file, click here: Remove Blanks With Dynamic Range Sample File To prevent this, either enter a default value in the data validation cell, Select the cell where you want the drop down list; On the Ribbon's� Click OK at the bottom of a page; A new section will appear called Content Types. Click on Document; You will notice the Title field listed as optional (just like other pieces of metadata, if you added custom metadata to a library) Once you click on the Title field above, you can switch it to Hidden. Then click OK; That’s all!


Default value for drop down list?, My QUESTION: How do you set a drop down list (which is created using data I want to default the value in the drop down box to the first value in the list, "(Select Region)". How do I I have seen this in other spreadsheets, but cannot tell how it was then be written to reset the cell to the default value you want. HTH Otto. Click on the Actions button and select Set Field in Current Item. Click on the field hyperlink and select Content Type. Click on the value hyperlink and select Existing item. Click on the Finish button. That’s it. While this looks like a large number of steps, it only takes about 5 minutes to set up.


Select Multiple Items from a Drop Down List in Excel, Select the cell or range of cells where you want the drop-down list to appear (C2 You May Also Like the Following Excel Tutorials: I am able to make multiple selections in the first column. Can I remove existing selection by clicking on the same value in drop down list. My drop down list has a default of “Select One”. In this post I will go through one of the common techniques of creating a custom list form using SharePoint Designer 2010. Custom-List I CHOOSE SITE ACTIONS > MORE OPTIONS I start off with a fresh site collection for which I have chosen the team site template as the top level site. This can be … Continue reading Creating a Custom List Form with SharePoint Designer 2010


Solved: Combo box in edit forms, I have been using drop downs and have them set up so that they default to a "-" on a NewForm, but show the current value when in EditForm, which I would like� Select any folder or document that you wish to delete and hit the Delete button from the keyboard and check if you are able to delete the file. Method 2: Sign-in or create another User account and check if you are able to see the delete option when you right click.