ASP.Net MVC, Partial Form + xVal

Sep 25, 2009 at 9:02 AM

Hi all

I'm running into issues with xVal when loading a partial form with Ajax.

First a little more infos:

I have a form for persons and therefore the class person. A person can have none to several addresses, the address is also a class.

When I load another address partial form via Ajax this partial form is not validated from xVal until a postback. When I load a person with an address and delete there a required field xVal is marking on client side.

Is there a way to get this fixed? I try to avoid postbacks whenever possible. I know that some might not have Javascript and they would always have postbacks, but on the side javascript is required anyways.

A similar question was asked some months ago, but no one answered.

Is there a proper way to get xVal working on rendered partial forms?

I put <%=Html.ClientSideValidation<Adresse>(ViewData["Prefix"] + "["+ ViewData["ListIndex"] +"]") %> on the partial form.

Thanks for any help

Griti

Sep 29, 2009 at 3:53 PM
Edited Sep 29, 2009 at 3:55 PM

Here is my generated html code of this form. Firefox however can validate the partial form (<span>... </span>).

Is there an issue with Internet explorer maybe?

 

 

<form action="/de-CH/MvcTestclient/CreatePerson" method="post">

<fieldset>

    <legend><b>Person</b></legend>

 

 

    <input id="prefix" value="Person" type="hidden">

 

    <input id="Person_SetById" value="0">

    <p>

 

        <label for="Person.Anrede">Anrede:</label>

        <select id="Person_Anrede_SetById" name="Person.Anrede.SetById"><option value="6">Herr</option>

<option value="7">Frau</option>

</select>

 

    </p>

    <p>

 

        <label for="Person.AnredeZusatz">AnredeZusatz:</label>

        <select id="Person_AnredeZusatz_SetById" name="Person.AnredeZusatz.SetById"><option value="">bitte wählen</option>

<option value="17">Doktor</option>

<option value="18">Professor</option>

</select>

 

    </p>

    <p>

 

        <label for="Person.Name">Name:</label>

        <input id="Person_Name" value="" type="text">

 

 

    </p>

    <p>

 

        <label for="Person.Vorname">Vorname:</label>

        <input id="Person_Vorname" value="">

 

    </p>

    <p>

 

        <label for="Person.DefaultLanguage">Sprache:</label>

        <select id="Person_LanguageSetById" name="Person.LanguageSetById"><option value="1">Deutsch (Schweiz)</option>

<option value="2">Englisch (GB)</option>

<option value="3">Französisch (Frankreich)</option>

</select>

 

    </p>

 

<fieldset>

 

    <legend><b>Adressen</b></legend>

 

    <div id="divAddressen">

    <span id="divAddresse">

    <input id="Person_Adressen[0]_SetById" name="Person.Adressen[0].SetById" value="0" type="hidden">

 

        <p><label for="Person.Adressen[0].Strasse">Strasse:</label>

        <input id="Person_Adressen[0]_Strasse" value=""> // new { id = ViewData["Prefix"].ToString().Replace('.', '_') + ViewData["ListIndex"].ToString() + "_Strasse" }%&gt;

        </p>

 

        <p><label for="Person.Adressen[0].Postfach">Postfach:</label>

        <input id="Person_Adressen[0]_Postfach" name="Person.Adressen[0].Postfach" value="" type="text">

        </p>

 

        <p><label for="Person.Adressen[0].PLZ">PLZ:</label>

        <input id="Person_Adressen[0]_PLZ" value="">

        </p>

 

        <p><label for="Person.Adressen[0].Ort">Ort:</label>

        <input id="Person_Adressen[0]_Ort" value="">// new { id = ViewData["Prefix"].ToString().Replace('.', '_') + ViewData["ListIndex"].ToString() + "_Ort" })%&gt;

        </p>

 

        <p><label for="Person.Adressen[0].Kanton">Kanton:</label>

        <select id="Person_Adressen[0]_Kanton_SetById" name="Person.Adressen[0].Kanton.SetById"><option value="2">Luzern</option>

<option value="3">Schwyz</option>

<option value="19">Uri</option>

</select></p>

 

        <p><label for="Person.Adressen[0].Land">Land:</label>

        <select id="Person_Adressen[0]_Land_SetById" name="Person.Adressen[0].Land.SetById"><option value="4">Schweiz</option>

<option value="5">England</option>

</select></p>

 

        <p><a href="#">

            Delete</a></p>

 

 

        <script type="text/javascript">xVal.AttachValidator("Person_Adressen[0]", {"Fields":[{"FieldName":"Id","FieldRules":[{"RuleName":"DataType","RuleParameters":{"Type":"Integer"}}]},{"FieldName":"Postfach","FieldRules":[{"RuleName":"Required","RuleParameters":{}}]},{"FieldName":"SetById","FieldRules":[{"RuleName":"DataType","RuleParameters":{"Type":"Integer"}}]},{"FieldName":"Strasse","FieldRules":[{"RuleName":"Required","RuleParameters":{},"Message":"Adresse: Bitte die Strasse ausfüllen!"},{"RuleName":"Required","RuleParameters":{},"Message":"EMPTY!!"}]}]}, {})</script>

 

</span></div>

    <input id="AdressListIndex" value="1">

    <a id="AddAdressLink" href="http://localhost:3832/de-CH/MvcTestclient/AddAddresse?Prefix=Person.Adressen&amp;ListDivName=divAddressen&amp;ListIndexName=AdressListIndex&amp;ListIndex=1" onclick="Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.insertAfter, updateTargetId: 'divAddressen', onSuccess: Function.createDelegate(this, function(){ListApi.ResetAddLink('AddAdressLink','AdressListIndex');}) });">Add Addresse</a>

</fieldset>

 

    <p></p>

 

<fieldset>

    <legend><b>Communication</b></legend>

 

    <div id="divCommunication">

 

    </div>

    <input id="CommunicationListIndex" value="-1">

    <a href="/de-CH/MvcTestclient/AddCommunication?Prefix=Person.CommunicationList&amp;ListDivName=divCommunication&amp;ListIndexName=CommunicationListIndex&amp;ListIndex=-1" onclick="Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.insertAfter, updateTargetId: 'divCommunication', onSuccess: Function.createDelegate(this, ListApi.ResetAddLink(event,'CommunicationListIndex')) });">Add Communication</a>

 

 

</fieldset>

 

    <p></p>

 

<div>

    <input id="isTeilnehmer" value="False">

    <a href="/de-CH/MvcTestclient/AddTeilnehmer/0?Prefix=Person.Teilnehmer" onclick="Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, updateTargetId: 'divTeilnehmer' });">Diese Person als Teilnehmer eröffnen</a>

 

</div>

 

   <p>

        <input value="Save">

    </p>

 

 

    <script type="text/javascript">xVal.AttachValidator("Person", {"Fields":[{"FieldName":"SetById","FieldRules":[{"RuleName":"DataType","RuleParameters":{"Type":"Integer"}}]},{"FieldName":"Id","FieldRules":[{"RuleName":"DataType","RuleParameters":{"Type":"Integer"}}]},{"FieldName":"LanguageSetById","FieldRules":[{"RuleName":"DataType","RuleParameters":{"Type":"Integer"}}]},{"FieldName":"Name","FieldRules":[{"RuleName":"StringLength","RuleParameters":{"MinLength":"1","MaxLength":"50"},"Message":"Der Name muss zwischen 1 und 50 Zeichen haben"},{"RuleName":"Required","RuleParameters":{},"Message":"EMPTY!!"}]},{"FieldName":"Vorname","FieldRules":[{"RuleName":"StringLength","RuleParameters":{"MinLength":"2","MaxLength":"50"},"Message":"Der Vorname muss zwischen 1 und 50 Zeichen haben"},{"RuleName":"Required","RuleParameters":{},"Message":"EMPTY!!"}]}]}, {})</script>

 

 

</fieldset>

<div>

    <a href="/de-CH/MvcTestclient">Back to List</a>

</div>

</form>