0

Need to change the user input right before going to the controller after form submit. The below solution changes the value but Modified input showing on the HTML Input field after replacing it from JavaScript.

Need alternative approach to change the value at Model level; therefore, the user cannot see the supplied input is changing.

<script>
$(document).ready(function () {    
    $("#submitButton").click(function (event) {
        var element = document.getElementById("RawHtml");
        var val = document.getElementById("RawHtml").value;
        document.getElementById("RawHtml").value = val.replace(/</g, '&lt;');
     });
});
</script>

@using (Html.BeginForm())
{
    <div class="field">
        @Html.LabelFor(model => model.Name)
        @Html.EditorFor(model => model.Name)
    </div>
    <div class="field">
        @Html.LabelFor(model => model.RawHtml)
        @Html.EditorFor(model => model.RawHtml)
    </div>
    <div>
        <input name="Submit" type="submit" id="submitButton" />
    </div>
}

1 Answer 1

1

You can try below:

If you don't want user to know that the supplied input is changed and then passed then change the supplied input when it hits the controller ActionResult.

Example:

[POST]
public ActionResult Demo(ModelName model){
    string finalHtml= model.RawHtml.Replace("value to be replaced","replacement value for existing value");
    //further use finalHtml to store in DB
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.