Hey guys, I’m doing an ASP.NET MVC project just for learning…and stuck in this…
I’m trying the get the value from the Html.TextBoxFor(model => model.ShortName) and show in the side http://www.blablabla.com/Value, it in the time of the user is typing.
I tried to add the AJAX library reference on the top:
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
<% Html.EnableClientValidation(); %>
Here goes the unsucceed code so far:
<div class="editor-label">
<%: Html.LabelFor(model => model.ShortName) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.ShortName) %>
<%: Html.ValidationMessageFor(model => model.ShortName) %>
This will be the name used for your short address
www.blablabla.com/<%: Model == null || string.IsNullOrEmpty(Model.ShortName) ? "shortName" : Model.ShortName %>
</div>
Thanks in advance
D.J is correct – you want to use Javascript for this. The
Modelobject is passed to the view from the server. Once the page is rendered, any interaction the user has with the page on the client has nothing to do with the Model (or the server). Only when the user sends a subsequent HTTP request (such as a POST by submitting a form) will a controller action be involved again.The model is used in your code example above to set the initial values of elements.
To do this you’re going to want to use javascript, and probably a library such as jQuery.
You can bind the keypress event of the textfield, and append its value to the div:
And then this to actually do the work: