MVC 3 Like Remote attribute
I nead function like Remote attribute but not for validation but to update other field example:
public class MyModel
{
public string Name{get;set;}
public string Surname{get;set;}
[RemoteUpdate("Name,Surname")]
public string FullName{get{return Name + " " + Surname开发者_如何学C}}
}
In this case FullName
will be only a label where. When someone focuses out of the Name
and Surname
field I want FullName
to be Updated.
Is this possible?
I'd recommend you to implement this completely on the client side using javascript.
Here is a really basic example for this using jQuery:
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js"></script>
</head>
<body>
<form>
Name:
<input id="name" type="text"/>
Surname:
<input id="surname" type="text"/>
<span id="fullname"></span>
</form>
<script type="text/javascript">
jQuery(document).ready(function() {
$('#surname, #name').keyup(function() { $('#fullname').text($('#name').val() + ' ' + $('#surname').val()); });
});
</script>
</html>
The keyup
event updates the span tag on every key stroke. If you want to update it only on leaving the textbox you can use the change
event.
You need to use either a jQuery .ajax() call or .get() operations when your client side textboxes loses focus (ie blurs) to then call your remote URL to do whatever you need. I didn't pass any parameters into the get request here (you can) and of course this is a generic example - but the idea is here on how you would do your remote call
<input id="surname" type="text"/>
<input id="fullname" type="text"/>
<script>
$('#surname').blur(function() {
$.get('remotefunctions/index', function(data) {
$('#fullname').html(data);
});
});
</script>
精彩评论