How do I update a property on the model based on the user selection?












0















I need to update the expiration date based on the action by the user. I want to handle this in the javascript if possible instead of sending a hidden field back to the controller.



In the model I have two dates.



    [DataType(DataType.Date)]
public DateTime ExpirationDate { get; set; }

[DataType(DataType.Date)]
public DateTime LastUpdatedDate { get; set; }


In the form, I show the current expiration date and this drop down select box



                <div class="controls col-md-6">
<label for="selExpirationPeriod">Select the expiration period</label>
</div>
<div class="controls col-md-6">
<select id="selExpirationPeriod" class="form-control">
<option value=30>30 days</option>
<option value=90>90 days</option>
<option value=182>Half year</option>
<option value=365 selected>Year</option>
</select>
</div>


The goal is to update the expiration date with the period chosen.



I have this routine where I want to make the change, on clicking the Update button.



function updateModel() {

alert($('#ExpirationDate'));
return true;
}


Instead of showing the expiration date, it shows "[object Object]"



I need something like



model.ExpirationDate = DateTime.Today + $selExpirationPeriod.value


I don't know how to access any of those three from javascript.



Summary: ExpirationDate is a property of the model which will be persisted in the database after it gets back to the server. I'd prefer to handle the calculation in the client if I can, just because I don't know how. It seems like it ought to be simple and I ought to learn it.



Update

Resolved. I had to add the hidden field so I could send the value back. As display only, it doesn't get returned.



        <div class="control-group">
<div class="controls col-md-6">
@Html.HiddenFor(cm => cm.ExpirationDate, new { id = "ExpirationDate" }) @*hidden input so that POST will send the data back*@
@Html.LabelFor(cm => cm.ExpirationDate)
@Html.DisplayFor(cm => cm.ExpirationDate, new { @class = "form-control" })
</div>
</div>


Then I had to update the field from the routine.



    document.getElementById("ExpirationDate").value = expireDate;









share|improve this question

























  • Are you want to update server-side property based from selected date option in <select> element and passed it back to view inside an element? I think you can use AJAX by handling change event then setting model.ExpirationDate = DateTime.Today.AddDays(x), but you need to provide relevant server-side codes and explain your objectives further.

    – Tetsuya Yamamoto
    Jan 2 at 2:58











  • I added a summary of what I'm trying to accomplish. If it isn't possible, then I'll just add another property to the model and pass it back that way. No need to complicate things with another call just to pass one property.

    – BWhite
    Jan 2 at 3:37













  • The $('#ExpirationDate') in the alert($('#ExpirationDate')); is the jQuery object for the #ExpirationDate element. If you want to get its value, you might need to do alert($('#ExpirationDate').val());

    – Tasos K.
    Jan 2 at 8:09











  • @BWhite, is your problem solved ? what is your opinion about the answer.

    – Aria
    Jan 2 at 16:20
















0















I need to update the expiration date based on the action by the user. I want to handle this in the javascript if possible instead of sending a hidden field back to the controller.



In the model I have two dates.



    [DataType(DataType.Date)]
public DateTime ExpirationDate { get; set; }

[DataType(DataType.Date)]
public DateTime LastUpdatedDate { get; set; }


In the form, I show the current expiration date and this drop down select box



                <div class="controls col-md-6">
<label for="selExpirationPeriod">Select the expiration period</label>
</div>
<div class="controls col-md-6">
<select id="selExpirationPeriod" class="form-control">
<option value=30>30 days</option>
<option value=90>90 days</option>
<option value=182>Half year</option>
<option value=365 selected>Year</option>
</select>
</div>


The goal is to update the expiration date with the period chosen.



I have this routine where I want to make the change, on clicking the Update button.



function updateModel() {

alert($('#ExpirationDate'));
return true;
}


Instead of showing the expiration date, it shows "[object Object]"



I need something like



model.ExpirationDate = DateTime.Today + $selExpirationPeriod.value


I don't know how to access any of those three from javascript.



Summary: ExpirationDate is a property of the model which will be persisted in the database after it gets back to the server. I'd prefer to handle the calculation in the client if I can, just because I don't know how. It seems like it ought to be simple and I ought to learn it.



Update

Resolved. I had to add the hidden field so I could send the value back. As display only, it doesn't get returned.



        <div class="control-group">
<div class="controls col-md-6">
@Html.HiddenFor(cm => cm.ExpirationDate, new { id = "ExpirationDate" }) @*hidden input so that POST will send the data back*@
@Html.LabelFor(cm => cm.ExpirationDate)
@Html.DisplayFor(cm => cm.ExpirationDate, new { @class = "form-control" })
</div>
</div>


Then I had to update the field from the routine.



    document.getElementById("ExpirationDate").value = expireDate;









share|improve this question

























  • Are you want to update server-side property based from selected date option in <select> element and passed it back to view inside an element? I think you can use AJAX by handling change event then setting model.ExpirationDate = DateTime.Today.AddDays(x), but you need to provide relevant server-side codes and explain your objectives further.

    – Tetsuya Yamamoto
    Jan 2 at 2:58











  • I added a summary of what I'm trying to accomplish. If it isn't possible, then I'll just add another property to the model and pass it back that way. No need to complicate things with another call just to pass one property.

    – BWhite
    Jan 2 at 3:37













  • The $('#ExpirationDate') in the alert($('#ExpirationDate')); is the jQuery object for the #ExpirationDate element. If you want to get its value, you might need to do alert($('#ExpirationDate').val());

    – Tasos K.
    Jan 2 at 8:09











  • @BWhite, is your problem solved ? what is your opinion about the answer.

    – Aria
    Jan 2 at 16:20














0












0








0


1






I need to update the expiration date based on the action by the user. I want to handle this in the javascript if possible instead of sending a hidden field back to the controller.



In the model I have two dates.



    [DataType(DataType.Date)]
public DateTime ExpirationDate { get; set; }

[DataType(DataType.Date)]
public DateTime LastUpdatedDate { get; set; }


In the form, I show the current expiration date and this drop down select box



                <div class="controls col-md-6">
<label for="selExpirationPeriod">Select the expiration period</label>
</div>
<div class="controls col-md-6">
<select id="selExpirationPeriod" class="form-control">
<option value=30>30 days</option>
<option value=90>90 days</option>
<option value=182>Half year</option>
<option value=365 selected>Year</option>
</select>
</div>


The goal is to update the expiration date with the period chosen.



I have this routine where I want to make the change, on clicking the Update button.



function updateModel() {

alert($('#ExpirationDate'));
return true;
}


Instead of showing the expiration date, it shows "[object Object]"



I need something like



model.ExpirationDate = DateTime.Today + $selExpirationPeriod.value


I don't know how to access any of those three from javascript.



Summary: ExpirationDate is a property of the model which will be persisted in the database after it gets back to the server. I'd prefer to handle the calculation in the client if I can, just because I don't know how. It seems like it ought to be simple and I ought to learn it.



Update

Resolved. I had to add the hidden field so I could send the value back. As display only, it doesn't get returned.



        <div class="control-group">
<div class="controls col-md-6">
@Html.HiddenFor(cm => cm.ExpirationDate, new { id = "ExpirationDate" }) @*hidden input so that POST will send the data back*@
@Html.LabelFor(cm => cm.ExpirationDate)
@Html.DisplayFor(cm => cm.ExpirationDate, new { @class = "form-control" })
</div>
</div>


Then I had to update the field from the routine.



    document.getElementById("ExpirationDate").value = expireDate;









share|improve this question
















I need to update the expiration date based on the action by the user. I want to handle this in the javascript if possible instead of sending a hidden field back to the controller.



In the model I have two dates.



    [DataType(DataType.Date)]
public DateTime ExpirationDate { get; set; }

[DataType(DataType.Date)]
public DateTime LastUpdatedDate { get; set; }


In the form, I show the current expiration date and this drop down select box



                <div class="controls col-md-6">
<label for="selExpirationPeriod">Select the expiration period</label>
</div>
<div class="controls col-md-6">
<select id="selExpirationPeriod" class="form-control">
<option value=30>30 days</option>
<option value=90>90 days</option>
<option value=182>Half year</option>
<option value=365 selected>Year</option>
</select>
</div>


The goal is to update the expiration date with the period chosen.



I have this routine where I want to make the change, on clicking the Update button.



function updateModel() {

alert($('#ExpirationDate'));
return true;
}


Instead of showing the expiration date, it shows "[object Object]"



I need something like



model.ExpirationDate = DateTime.Today + $selExpirationPeriod.value


I don't know how to access any of those three from javascript.



Summary: ExpirationDate is a property of the model which will be persisted in the database after it gets back to the server. I'd prefer to handle the calculation in the client if I can, just because I don't know how. It seems like it ought to be simple and I ought to learn it.



Update

Resolved. I had to add the hidden field so I could send the value back. As display only, it doesn't get returned.



        <div class="control-group">
<div class="controls col-md-6">
@Html.HiddenFor(cm => cm.ExpirationDate, new { id = "ExpirationDate" }) @*hidden input so that POST will send the data back*@
@Html.LabelFor(cm => cm.ExpirationDate)
@Html.DisplayFor(cm => cm.ExpirationDate, new { @class = "form-control" })
</div>
</div>


Then I had to update the field from the routine.



    document.getElementById("ExpirationDate").value = expireDate;






javascript c# asp.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 3:50







BWhite

















asked Jan 1 at 21:46









BWhiteBWhite

121110




121110













  • Are you want to update server-side property based from selected date option in <select> element and passed it back to view inside an element? I think you can use AJAX by handling change event then setting model.ExpirationDate = DateTime.Today.AddDays(x), but you need to provide relevant server-side codes and explain your objectives further.

    – Tetsuya Yamamoto
    Jan 2 at 2:58











  • I added a summary of what I'm trying to accomplish. If it isn't possible, then I'll just add another property to the model and pass it back that way. No need to complicate things with another call just to pass one property.

    – BWhite
    Jan 2 at 3:37













  • The $('#ExpirationDate') in the alert($('#ExpirationDate')); is the jQuery object for the #ExpirationDate element. If you want to get its value, you might need to do alert($('#ExpirationDate').val());

    – Tasos K.
    Jan 2 at 8:09











  • @BWhite, is your problem solved ? what is your opinion about the answer.

    – Aria
    Jan 2 at 16:20



















  • Are you want to update server-side property based from selected date option in <select> element and passed it back to view inside an element? I think you can use AJAX by handling change event then setting model.ExpirationDate = DateTime.Today.AddDays(x), but you need to provide relevant server-side codes and explain your objectives further.

    – Tetsuya Yamamoto
    Jan 2 at 2:58











  • I added a summary of what I'm trying to accomplish. If it isn't possible, then I'll just add another property to the model and pass it back that way. No need to complicate things with another call just to pass one property.

    – BWhite
    Jan 2 at 3:37













  • The $('#ExpirationDate') in the alert($('#ExpirationDate')); is the jQuery object for the #ExpirationDate element. If you want to get its value, you might need to do alert($('#ExpirationDate').val());

    – Tasos K.
    Jan 2 at 8:09











  • @BWhite, is your problem solved ? what is your opinion about the answer.

    – Aria
    Jan 2 at 16:20

















Are you want to update server-side property based from selected date option in <select> element and passed it back to view inside an element? I think you can use AJAX by handling change event then setting model.ExpirationDate = DateTime.Today.AddDays(x), but you need to provide relevant server-side codes and explain your objectives further.

– Tetsuya Yamamoto
Jan 2 at 2:58





Are you want to update server-side property based from selected date option in <select> element and passed it back to view inside an element? I think you can use AJAX by handling change event then setting model.ExpirationDate = DateTime.Today.AddDays(x), but you need to provide relevant server-side codes and explain your objectives further.

– Tetsuya Yamamoto
Jan 2 at 2:58













I added a summary of what I'm trying to accomplish. If it isn't possible, then I'll just add another property to the model and pass it back that way. No need to complicate things with another call just to pass one property.

– BWhite
Jan 2 at 3:37







I added a summary of what I'm trying to accomplish. If it isn't possible, then I'll just add another property to the model and pass it back that way. No need to complicate things with another call just to pass one property.

– BWhite
Jan 2 at 3:37















The $('#ExpirationDate') in the alert($('#ExpirationDate')); is the jQuery object for the #ExpirationDate element. If you want to get its value, you might need to do alert($('#ExpirationDate').val());

– Tasos K.
Jan 2 at 8:09





The $('#ExpirationDate') in the alert($('#ExpirationDate')); is the jQuery object for the #ExpirationDate element. If you want to get its value, you might need to do alert($('#ExpirationDate').val());

– Tasos K.
Jan 2 at 8:09













@BWhite, is your problem solved ? what is your opinion about the answer.

– Aria
Jan 2 at 16:20





@BWhite, is your problem solved ? what is your opinion about the answer.

– Aria
Jan 2 at 16:20












3 Answers
3






active

oldest

votes


















1














I think you wand calculate the expire date based on user selection of selExpirationPeriod and from what you want something like model.ExpirationDate = DateTime.Today + $selExpirationPeriod.value you should add that days number to your date



I have provided pure JavaScript extension named addDaysToDate() to add day number to DateTime.Now as sample you can improve it, any way in expBtn click you have expireDate you can pass it to server by that Ajax.




Date.prototype.addDaysToDate = function(days) { 
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
var expBtn = document.getElementById('makeExpDate');
expBtn.addEventListener("click",function(e){
var option = document.getElementById("selExpirationPeriod");
var selectedDay = option.options[option.selectedIndex].value;
var date = new Date();
var expireDate = date.addDaysToDate(selectedDay);
alert(expireDate);
},false);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="controls col-md-6">
<label for="selExpirationPeriod">Select the expiration period</label>
</div>
<div class="controls col-md-6">
<select id="selExpirationPeriod" class="form-control">
<option value=30>30 days</option>
<option value=90>90 days</option>
<option value=182>Half year</option>
<option value=365 selected>Year</option>
</select>
</div>
<button id='makeExpDate'>Expire Date</button>








share|improve this answer
























  • Interestingly, selecting 365 gives a result of '6/22/2025'. Turns out that it is interpreting the 365 as a string. Days + 0 = 3650. So that was easily fixed once we found it.

    – BWhite
    Jan 2 at 23:58






  • 1





    This one got me most of the way there, so I marking it as the answer.

    – BWhite
    Jan 3 at 3:51











  • Yeah it may have some little problems, I am glad it helped you to find the clue.

    – Aria
    Jan 4 at 8:21



















0















You are not able to update server side property on client side. you have to make an ajax call to update Expiration date field or etc.







share|improve this answer
























  • That sounds perfectly reasonable and I would believe you except that we are updating every other field in the model. This is the UpdateClient form. It seems like I update the property on the model somehow and post it back to the controller.

    – BWhite
    Jan 2 at 17:03











  • yeah you are write but he mention in question I need something like model.ExpirationDate = DateTime.Today + $selExpirationPeriod.value so i answer acordingly

    – Hafiz Asad
    Jan 3 at 7:44





















0














The final routine looked like this:



Date.prototype.addDaysToDate = function (days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + (days * 1)); // coerce days to be an int
return date;
}
function updateModel() {
var option = document.getElementById("selExpirationPeriod");
var selectedDay = option.options[option.selectedIndex].value;
var date = new Date();
date.setHours(0, 0, 0, 0); // remove the time portion

var expireDate = date.addDaysToDate(selectedDay);
document.getElementById("ExpirationDate").value = expireDate;
return true;
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999196%2fhow-do-i-update-a-property-on-the-model-based-on-the-user-selection%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I think you wand calculate the expire date based on user selection of selExpirationPeriod and from what you want something like model.ExpirationDate = DateTime.Today + $selExpirationPeriod.value you should add that days number to your date



    I have provided pure JavaScript extension named addDaysToDate() to add day number to DateTime.Now as sample you can improve it, any way in expBtn click you have expireDate you can pass it to server by that Ajax.




    Date.prototype.addDaysToDate = function(days) { 
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
    }
    var expBtn = document.getElementById('makeExpDate');
    expBtn.addEventListener("click",function(e){
    var option = document.getElementById("selExpirationPeriod");
    var selectedDay = option.options[option.selectedIndex].value;
    var date = new Date();
    var expireDate = date.addDaysToDate(selectedDay);
    alert(expireDate);
    },false);

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="controls col-md-6">
    <label for="selExpirationPeriod">Select the expiration period</label>
    </div>
    <div class="controls col-md-6">
    <select id="selExpirationPeriod" class="form-control">
    <option value=30>30 days</option>
    <option value=90>90 days</option>
    <option value=182>Half year</option>
    <option value=365 selected>Year</option>
    </select>
    </div>
    <button id='makeExpDate'>Expire Date</button>








    share|improve this answer
























    • Interestingly, selecting 365 gives a result of '6/22/2025'. Turns out that it is interpreting the 365 as a string. Days + 0 = 3650. So that was easily fixed once we found it.

      – BWhite
      Jan 2 at 23:58






    • 1





      This one got me most of the way there, so I marking it as the answer.

      – BWhite
      Jan 3 at 3:51











    • Yeah it may have some little problems, I am glad it helped you to find the clue.

      – Aria
      Jan 4 at 8:21
















    1














    I think you wand calculate the expire date based on user selection of selExpirationPeriod and from what you want something like model.ExpirationDate = DateTime.Today + $selExpirationPeriod.value you should add that days number to your date



    I have provided pure JavaScript extension named addDaysToDate() to add day number to DateTime.Now as sample you can improve it, any way in expBtn click you have expireDate you can pass it to server by that Ajax.




    Date.prototype.addDaysToDate = function(days) { 
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
    }
    var expBtn = document.getElementById('makeExpDate');
    expBtn.addEventListener("click",function(e){
    var option = document.getElementById("selExpirationPeriod");
    var selectedDay = option.options[option.selectedIndex].value;
    var date = new Date();
    var expireDate = date.addDaysToDate(selectedDay);
    alert(expireDate);
    },false);

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="controls col-md-6">
    <label for="selExpirationPeriod">Select the expiration period</label>
    </div>
    <div class="controls col-md-6">
    <select id="selExpirationPeriod" class="form-control">
    <option value=30>30 days</option>
    <option value=90>90 days</option>
    <option value=182>Half year</option>
    <option value=365 selected>Year</option>
    </select>
    </div>
    <button id='makeExpDate'>Expire Date</button>








    share|improve this answer
























    • Interestingly, selecting 365 gives a result of '6/22/2025'. Turns out that it is interpreting the 365 as a string. Days + 0 = 3650. So that was easily fixed once we found it.

      – BWhite
      Jan 2 at 23:58






    • 1





      This one got me most of the way there, so I marking it as the answer.

      – BWhite
      Jan 3 at 3:51











    • Yeah it may have some little problems, I am glad it helped you to find the clue.

      – Aria
      Jan 4 at 8:21














    1












    1








    1







    I think you wand calculate the expire date based on user selection of selExpirationPeriod and from what you want something like model.ExpirationDate = DateTime.Today + $selExpirationPeriod.value you should add that days number to your date



    I have provided pure JavaScript extension named addDaysToDate() to add day number to DateTime.Now as sample you can improve it, any way in expBtn click you have expireDate you can pass it to server by that Ajax.




    Date.prototype.addDaysToDate = function(days) { 
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
    }
    var expBtn = document.getElementById('makeExpDate');
    expBtn.addEventListener("click",function(e){
    var option = document.getElementById("selExpirationPeriod");
    var selectedDay = option.options[option.selectedIndex].value;
    var date = new Date();
    var expireDate = date.addDaysToDate(selectedDay);
    alert(expireDate);
    },false);

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="controls col-md-6">
    <label for="selExpirationPeriod">Select the expiration period</label>
    </div>
    <div class="controls col-md-6">
    <select id="selExpirationPeriod" class="form-control">
    <option value=30>30 days</option>
    <option value=90>90 days</option>
    <option value=182>Half year</option>
    <option value=365 selected>Year</option>
    </select>
    </div>
    <button id='makeExpDate'>Expire Date</button>








    share|improve this answer













    I think you wand calculate the expire date based on user selection of selExpirationPeriod and from what you want something like model.ExpirationDate = DateTime.Today + $selExpirationPeriod.value you should add that days number to your date



    I have provided pure JavaScript extension named addDaysToDate() to add day number to DateTime.Now as sample you can improve it, any way in expBtn click you have expireDate you can pass it to server by that Ajax.




    Date.prototype.addDaysToDate = function(days) { 
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
    }
    var expBtn = document.getElementById('makeExpDate');
    expBtn.addEventListener("click",function(e){
    var option = document.getElementById("selExpirationPeriod");
    var selectedDay = option.options[option.selectedIndex].value;
    var date = new Date();
    var expireDate = date.addDaysToDate(selectedDay);
    alert(expireDate);
    },false);

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="controls col-md-6">
    <label for="selExpirationPeriod">Select the expiration period</label>
    </div>
    <div class="controls col-md-6">
    <select id="selExpirationPeriod" class="form-control">
    <option value=30>30 days</option>
    <option value=90>90 days</option>
    <option value=182>Half year</option>
    <option value=365 selected>Year</option>
    </select>
    </div>
    <button id='makeExpDate'>Expire Date</button>








    Date.prototype.addDaysToDate = function(days) { 
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
    }
    var expBtn = document.getElementById('makeExpDate');
    expBtn.addEventListener("click",function(e){
    var option = document.getElementById("selExpirationPeriod");
    var selectedDay = option.options[option.selectedIndex].value;
    var date = new Date();
    var expireDate = date.addDaysToDate(selectedDay);
    alert(expireDate);
    },false);

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="controls col-md-6">
    <label for="selExpirationPeriod">Select the expiration period</label>
    </div>
    <div class="controls col-md-6">
    <select id="selExpirationPeriod" class="form-control">
    <option value=30>30 days</option>
    <option value=90>90 days</option>
    <option value=182>Half year</option>
    <option value=365 selected>Year</option>
    </select>
    </div>
    <button id='makeExpDate'>Expire Date</button>





    Date.prototype.addDaysToDate = function(days) { 
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
    }
    var expBtn = document.getElementById('makeExpDate');
    expBtn.addEventListener("click",function(e){
    var option = document.getElementById("selExpirationPeriod");
    var selectedDay = option.options[option.selectedIndex].value;
    var date = new Date();
    var expireDate = date.addDaysToDate(selectedDay);
    alert(expireDate);
    },false);

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="controls col-md-6">
    <label for="selExpirationPeriod">Select the expiration period</label>
    </div>
    <div class="controls col-md-6">
    <select id="selExpirationPeriod" class="form-control">
    <option value=30>30 days</option>
    <option value=90>90 days</option>
    <option value=182>Half year</option>
    <option value=365 selected>Year</option>
    </select>
    </div>
    <button id='makeExpDate'>Expire Date</button>






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 2 at 10:38









    AriaAria

    2,7151933




    2,7151933













    • Interestingly, selecting 365 gives a result of '6/22/2025'. Turns out that it is interpreting the 365 as a string. Days + 0 = 3650. So that was easily fixed once we found it.

      – BWhite
      Jan 2 at 23:58






    • 1





      This one got me most of the way there, so I marking it as the answer.

      – BWhite
      Jan 3 at 3:51











    • Yeah it may have some little problems, I am glad it helped you to find the clue.

      – Aria
      Jan 4 at 8:21



















    • Interestingly, selecting 365 gives a result of '6/22/2025'. Turns out that it is interpreting the 365 as a string. Days + 0 = 3650. So that was easily fixed once we found it.

      – BWhite
      Jan 2 at 23:58






    • 1





      This one got me most of the way there, so I marking it as the answer.

      – BWhite
      Jan 3 at 3:51











    • Yeah it may have some little problems, I am glad it helped you to find the clue.

      – Aria
      Jan 4 at 8:21

















    Interestingly, selecting 365 gives a result of '6/22/2025'. Turns out that it is interpreting the 365 as a string. Days + 0 = 3650. So that was easily fixed once we found it.

    – BWhite
    Jan 2 at 23:58





    Interestingly, selecting 365 gives a result of '6/22/2025'. Turns out that it is interpreting the 365 as a string. Days + 0 = 3650. So that was easily fixed once we found it.

    – BWhite
    Jan 2 at 23:58




    1




    1





    This one got me most of the way there, so I marking it as the answer.

    – BWhite
    Jan 3 at 3:51





    This one got me most of the way there, so I marking it as the answer.

    – BWhite
    Jan 3 at 3:51













    Yeah it may have some little problems, I am glad it helped you to find the clue.

    – Aria
    Jan 4 at 8:21





    Yeah it may have some little problems, I am glad it helped you to find the clue.

    – Aria
    Jan 4 at 8:21













    0















    You are not able to update server side property on client side. you have to make an ajax call to update Expiration date field or etc.







    share|improve this answer
























    • That sounds perfectly reasonable and I would believe you except that we are updating every other field in the model. This is the UpdateClient form. It seems like I update the property on the model somehow and post it back to the controller.

      – BWhite
      Jan 2 at 17:03











    • yeah you are write but he mention in question I need something like model.ExpirationDate = DateTime.Today + $selExpirationPeriod.value so i answer acordingly

      – Hafiz Asad
      Jan 3 at 7:44


















    0















    You are not able to update server side property on client side. you have to make an ajax call to update Expiration date field or etc.







    share|improve this answer
























    • That sounds perfectly reasonable and I would believe you except that we are updating every other field in the model. This is the UpdateClient form. It seems like I update the property on the model somehow and post it back to the controller.

      – BWhite
      Jan 2 at 17:03











    • yeah you are write but he mention in question I need something like model.ExpirationDate = DateTime.Today + $selExpirationPeriod.value so i answer acordingly

      – Hafiz Asad
      Jan 3 at 7:44
















    0












    0








    0








    You are not able to update server side property on client side. you have to make an ajax call to update Expiration date field or etc.







    share|improve this answer














    You are not able to update server side property on client side. you have to make an ajax call to update Expiration date field or etc.








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 2 at 10:15









    Hafiz AsadHafiz Asad

    847714




    847714













    • That sounds perfectly reasonable and I would believe you except that we are updating every other field in the model. This is the UpdateClient form. It seems like I update the property on the model somehow and post it back to the controller.

      – BWhite
      Jan 2 at 17:03











    • yeah you are write but he mention in question I need something like model.ExpirationDate = DateTime.Today + $selExpirationPeriod.value so i answer acordingly

      – Hafiz Asad
      Jan 3 at 7:44





















    • That sounds perfectly reasonable and I would believe you except that we are updating every other field in the model. This is the UpdateClient form. It seems like I update the property on the model somehow and post it back to the controller.

      – BWhite
      Jan 2 at 17:03











    • yeah you are write but he mention in question I need something like model.ExpirationDate = DateTime.Today + $selExpirationPeriod.value so i answer acordingly

      – Hafiz Asad
      Jan 3 at 7:44



















    That sounds perfectly reasonable and I would believe you except that we are updating every other field in the model. This is the UpdateClient form. It seems like I update the property on the model somehow and post it back to the controller.

    – BWhite
    Jan 2 at 17:03





    That sounds perfectly reasonable and I would believe you except that we are updating every other field in the model. This is the UpdateClient form. It seems like I update the property on the model somehow and post it back to the controller.

    – BWhite
    Jan 2 at 17:03













    yeah you are write but he mention in question I need something like model.ExpirationDate = DateTime.Today + $selExpirationPeriod.value so i answer acordingly

    – Hafiz Asad
    Jan 3 at 7:44







    yeah you are write but he mention in question I need something like model.ExpirationDate = DateTime.Today + $selExpirationPeriod.value so i answer acordingly

    – Hafiz Asad
    Jan 3 at 7:44













    0














    The final routine looked like this:



    Date.prototype.addDaysToDate = function (days) {
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + (days * 1)); // coerce days to be an int
    return date;
    }
    function updateModel() {
    var option = document.getElementById("selExpirationPeriod");
    var selectedDay = option.options[option.selectedIndex].value;
    var date = new Date();
    date.setHours(0, 0, 0, 0); // remove the time portion

    var expireDate = date.addDaysToDate(selectedDay);
    document.getElementById("ExpirationDate").value = expireDate;
    return true;
    }





    share|improve this answer




























      0














      The final routine looked like this:



      Date.prototype.addDaysToDate = function (days) {
      var date = new Date(this.valueOf());
      date.setDate(date.getDate() + (days * 1)); // coerce days to be an int
      return date;
      }
      function updateModel() {
      var option = document.getElementById("selExpirationPeriod");
      var selectedDay = option.options[option.selectedIndex].value;
      var date = new Date();
      date.setHours(0, 0, 0, 0); // remove the time portion

      var expireDate = date.addDaysToDate(selectedDay);
      document.getElementById("ExpirationDate").value = expireDate;
      return true;
      }





      share|improve this answer


























        0












        0








        0







        The final routine looked like this:



        Date.prototype.addDaysToDate = function (days) {
        var date = new Date(this.valueOf());
        date.setDate(date.getDate() + (days * 1)); // coerce days to be an int
        return date;
        }
        function updateModel() {
        var option = document.getElementById("selExpirationPeriod");
        var selectedDay = option.options[option.selectedIndex].value;
        var date = new Date();
        date.setHours(0, 0, 0, 0); // remove the time portion

        var expireDate = date.addDaysToDate(selectedDay);
        document.getElementById("ExpirationDate").value = expireDate;
        return true;
        }





        share|improve this answer













        The final routine looked like this:



        Date.prototype.addDaysToDate = function (days) {
        var date = new Date(this.valueOf());
        date.setDate(date.getDate() + (days * 1)); // coerce days to be an int
        return date;
        }
        function updateModel() {
        var option = document.getElementById("selExpirationPeriod");
        var selectedDay = option.options[option.selectedIndex].value;
        var date = new Date();
        date.setHours(0, 0, 0, 0); // remove the time portion

        var expireDate = date.addDaysToDate(selectedDay);
        document.getElementById("ExpirationDate").value = expireDate;
        return true;
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 3:53









        BWhiteBWhite

        121110




        121110






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999196%2fhow-do-i-update-a-property-on-the-model-based-on-the-user-selection%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas