i have 2 radiobuttons - no , yes. , want save state of radio buttons. current situation state of radiobutton yes or no after save. if select no , save , return page state still on yes. have in view:
<div class="form-group"> @html.label(resources.entity.product.ogonefutureprice, new { @class = "text-bold control-label col-md-2" }) <div class="col-lg-6 col-md-8 col-sm-10 "> @html.label(resources.entity.product.showfuturepriceyes) @html.radiobuttonfor(model => model.ogone.futureproductprice, true, new { @class = "futurenewproductprice" }) @html.label(resources.entity.product.showfuturepriceno) @html.radiobuttonfor(model => model.ogone.futureproductprice, false, new { @class = "futurenewproductprice" }) </div> </div> <div class="form-group" id="newproductpriceview"> <div class="form-group"> @html.labelfor(model => model.ogone.newproductpriceview, new { @class = "text-bold control-label col-md-2" }) <div class="col-lg-6 col-md-8 col-sm-10 "> <input type="text" pattern="\d+([\.,]\d{2})?" value="@model.ogone.newproductpriceview" name="@html.namefor(model=>model.ogone.newproductpriceview)" class="form-control" id="@html.namefor(model => model.ogone.newproductpriceview).tostring().replace('.','_')" placeholder="@resources.entity.product.ogoneproductpriceplaceholder" /> @html.validationmessagefor(model => model.ogone.newproductpriceview) </div> </div> <div class="form-group"> @html.labelfor(model => model.ogone.newproductpricedateview, new { @class = "text-bold control-label col-md-2" }) <div class="col-lg-6 col-md-8 col-sm-10 "> <input type="text" value="@model.ogone.newproductpricedateview" id="@html.namefor(model => model.ogone.newproductpricedateview).tostring().replace('.','_')" class="form-control datepicker" name="@html.namefor(model => model.ogone.newproductpricedateview)" data-val="true" data-pattern="@viewhelper.getjslocaledateformat()" data-val-checknewproductpricedate="@resources.entity.product.ogonenonewpricedate" data-val-validatenewproductpricedate="@resources.entity.product.ogoneinvalidnewpricedate" /> @html.validationmessagefor(model => model.ogone.newproductpricedateview) </div> </div> </div> <script> (function ($) { $.validator.addmethod("checknewproductpricedate", function (val, el) { if ($("#ogone_newproductpriceview").val().length != 0) return val.length != 0; return true; }); $.validator.addmethod("validatenewproductpricedate", function (val, el) { // checken of de datum in de toekomst ligt... var v = getdatefrominput(val, el); return (v > new date()); }); $.validator.unobtrusive.adapters.addbool("checknewproductpricedate"); $.validator.unobtrusive.adapters.addbool("validatenewproductpricedate"); })(jquery); </script> </div> @if (model.ogone.futureproductprice == false) { <script> $(document).ready(function () { $('#newproductpriceview').hide(); }); </script> } this javascript:
$(".futurenewproductprice").change(function () { if ($(this).val() == "true") { $('#newproductpriceview').show(); } else { $('#newproductpriceview').hide(); } }); and logic:
internal void deserialize(editproductmodel model) { xdocument settings = xdocument.parse(model.product.paymentsettings); float price; xelement settingselement = settings.root.element("productprice"); if (settingselement != null && float.tryparse(settingselement.value, system.globalization.numberstyles.currency, system.globalization.cultureinfo.invariantculture, out price)) model.ogone.productprice = price; settingselement = settings.root.element("newproductprice"); if (settingselement != null) { xelement newpriceelement = settingselement.element("productprice"); if (newpriceelement != null && float.tryparse(newpriceelement.value, system.globalization.numberstyles.currency, system.globalization.cultureinfo.invariantculture, out price)) model.ogone.newproductprice = price; newpriceelement = settingselement.element("effectivedate"); datetime dt; if (newpriceelement != null && datetime.tryparseexact(newpriceelement.value, "yyyy-mm-dd", cultureinfo.invariantculture, datetimestyles.none, out dt)) model.ogone.newproductpricedate = dt; xelement futureproductprice = settings.root.element("futureproductprice"); if (futureproductprice != null && !string.isnullorwhitespace(futureproductprice.value)) futureproductprice = xmlconvert.toboolean(futureproductprice.value); else futureproductprice = true; } } and responsible radiobuttons:
xelement futureproductprice = settings.root.element("futureproductprice"); if (futureproductprice != null && !string.isnullorwhitespace(futureproductprice.value)) futureproductprice = xmlconvert.toboolean(futureproductprice.value); else futureproductprice = true; but everytime choose(yes or no) , always:
futureproductprice = true; and serialize method:
internal string serialize(editproductmodel model) { xdocument settings = new xdocument(new xelement("settings")); settings.root.add(new xelement("productprice", model.ogone.productprice.tostring("f2", cultureinfo.invariantculture))); if (newproductprice.hasvalue) { if (!newproductpricedate.hasvalue) throw new exception("no date set new product price"); settings.root.add( new xelement("newproductprice", new xelement("productprice", model.ogone.newproductprice.value.tostring("f2", cultureinfo.invariantculture)), new xelement("effectivedate", model.ogone.newproductpricedate.value.tostring("yyyy-mm-dd", cultureinfo.invariantculture)), new xelement("futureproductprice", futureproductprice ) ) ); } return settings.tostring(saveoptions.omitduplicatenamespaces); } i try in serialize method:
string ischeckpdf = httpcontext.current.request.form["model.ogone.futureproductprice"]; futureproductprice = ischeckpdf.equals("true", stringcomparison.ordinalignorecase); i try this:
xelement futureproductprice = settings.root.element("futureproductprice"); futureproductprice.setattributevalue("futureproductprice", true); if (futureproductprice != null && !string.isnullorwhitespace(futureproductprice.value) && futureproductprice == false) futureproductprice = xmlconvert.toboolean(futureproductprice.value); //futureproductprice = true; else futureproductprice = true; but message:
object reference not set instance of object. on line:
futureproductprice.setattributevalue("futureproductprice", true); if this:
xelement futureproductprice = settings.root.element("futureproductprice"); futureproductprice.setvalue("futureproductprice"); if (futureproductprice != null && !string.isnullorwhitespace(futureproductprice.value) && futureproductprice == false) futureproductprice = xmlconvert.toboolean(futureproductprice.value); //futureproductprice = true; else futureproductprice = true; i this:
object reference not set instance of object. on line:
futureproductprice.setvalue("futureproductprice"); if this:
xelement futureproductprice = settings.root.element("futureproductprice"); futureproductprice.setvalue("true"); if (futureproductprice != null && !string.isnullorwhitespace(futureproductprice.value)) futureproductprice = xmlconvert.toboolean(futureproductprice.value); else futureproductprice = true; i warning:
object reference not set instance of object. this complete class:
public class ogonesettings { [display(name = "ogoneproductprice", resourcetype = typeof(resources.entity.product))] public string productpriceview { { return this.productprice.tostring("f2", cultureinfo.currentuiculture); } set { if (string.isnullorwhitespace(value)) { this.productprice = 0; return; } float p; string inp = value.replace(',', '.'); if (!float.tryparse(inp, numberstyles.float, cultureinfo.invariantculture, out p)) throw new exception("invalid price: " + value); this.productprice = p; } } /* checkout id wordt bepaald door de omgeving: production of test. [display(name = "ogonecheckoutid", resourcetype = typeof(resources.entity.product))] public string checkoutid { get; set; } */ [display(name = "newproductprice", resourcetype = typeof(resources.entity.product))] public string newproductpriceview { { return this.newproductprice.hasvalue ? this.newproductprice.value.tostring("f2", cultureinfo.currentuiculture) : ""; } set { if (string.isnullorwhitespace(value)) { this.newproductprice = null; return; } float p; string inp = value.replace(',', '.'); if (!float.tryparse(inp, numberstyles.float, cultureinfo.invariantculture, out p)) throw new exception("invalid price: " + value); this.newproductprice = p; } } private float? newproductprice; private float productprice; private datetime? newproductpricedate; public bool futureproductprice { get; set; } [display(name = "newproductpricedate", resourcetype = typeof(resources.entity.product))] [newproductpricevalidation(errormessageresourcetype = typeof(resources.entity.product), errormessageresourcename = "ogonenonewpricedate")] [datetimevalidation(senecaformsserver.sfshelpers.validationtype.futuredate, null, errormessageresourcetype = typeof(resources.entity.product), errormessageresourcename = "ogoneinvalidnewpricedate")] public string newproductpricedateview { { return newproductpricedate == null ? "" : newproductpricedate.value.tostring(cultureinfo.currentuiculture.datetimeformat.shortdatepattern, cultureinfo.currentuiculture); } set { if (string.isnullorempty(value)) { newproductpricedate = null; return; } string pattern = cultureinfo.currentuiculture.datetimeformat.shortdatepattern.replace(' ', '-').replace('/', '-').replace('.', '-'); string clientvalue = value.replace(' ', '-').replace('/', '-').replace('.', '-'); datetime dt; if (!datetime.tryparseexact(clientvalue, pattern, cultureinfo.currentuiculture, datetimestyles.none, out dt)) throw new exception("invalid date: " + value); newproductpricedate = dt; } } internal string serialize(editproductmodel model) { xdocument settings = new xdocument(new xelement("settings")); settings.root.add(new xelement("productprice", model.ogone.productprice.tostring("f2", cultureinfo.invariantculture))); if (newproductprice.hasvalue) { if (!newproductpricedate.hasvalue) throw new exception("no date set new product price"); settings.root.add( new xelement("newproductprice", new xelement("productprice", model.ogone.newproductprice.value.tostring("f2", cultureinfo.invariantculture)), new xelement("effectivedate", model.ogone.newproductpricedate.value.tostring("yyyy-mm-dd", cultureinfo.invariantculture)), new xelement("futureproductprice", true ) ) ); } return settings.tostring(saveoptions.omitduplicatenamespaces); } internal void deserialize(editproductmodel model) { xdocument settings = xdocument.parse(model.product.paymentsettings); float price; xelement settingselement = settings.root.element("productprice"); if (settingselement != null && float.tryparse(settingselement.value, system.globalization.numberstyles.currency, system.globalization.cultureinfo.invariantculture, out price)) model.ogone.productprice = price; settingselement = settings.root.element("newproductprice"); if (settingselement != null) { xelement newpriceelement = settingselement.element("productprice"); if (newpriceelement != null && float.tryparse(newpriceelement.value, system.globalization.numberstyles.currency, system.globalization.cultureinfo.invariantculture, out price)) model.ogone.newproductprice = price; newpriceelement = settingselement.element("effectivedate"); datetime dt; if (newpriceelement != null && datetime.tryparseexact(newpriceelement.value, "yyyy-mm-dd", cultureinfo.invariantculture, datetimestyles.none, out dt)) model.ogone.newproductpricedate = dt; xelement futureproductprice = settings.root.element("futureproductprice"); if (futureproductprice != null && !string.isnullorwhitespace(futureproductprice.value)) //futureproductprice.setvalue("false"); futureproductprice = xmlconvert.toboolean(futureproductprice.value); else futureproductprice = true; } } } public class newproductpricevalidation : validationattribute { public newproductpricevalidation() { } protected override validationresult isvalid(object value, validationcontext validationcontext) { ogonesettings o = validationcontext.objectinstance ogonesettings; if (string.isnullorempty(o.newproductpriceview)) return null; if (string.isnullorwhitespace(o.newproductpricedateview)) return new validationresult(this.formaterrormessage(validationcontext.displayname)); return null; } }
since cannot comment on original question i'll have write here answer: since you're getting null result futureproductprice, wouldn't mean :
if (futureproductprice != null && !string.isnullorwhitespace(futureproductprice.value)) condition never verified , therefore 'futureproductprice.value' not set?
try editing part of code replacing futureproductprice true, so:
settings.root.add( new xelement("newproductprice", new xelement("productprice", model.ogone.newproductprice.value.tostring("f2", cultureinfo.invariantculture)), new xelement("effectivedate", model.ogone.newproductpricedate.value.tostring("yyyy-mm-dd", cultureinfo.invariantculture)), new xelement("futureproductprice", true ) ) );
Comments
Post a Comment