Reputation: 1035
I have 2 field in my table : Name , Family . I want to insert both of these items with one editor . In asp.net webforms we insert it like this :
sqlcommand1.parameters.addwithvalue("Field", txtName.text + txtFamily.text);
I do this in MVC :
@Html.EditorFor((model => model.Name) && (model => model.Family))
Now I want to know how can I do this ?
Upvotes: 1
Views: 214
Reputation: 102723
I would add another property in your model, FullName:
public string FullName
{
get { return Name + " " + Family; }
set
{
string[] names = value.Split(' ');
Name = names[0];
if (names.Length > 1) Family = string.Join(" ", names.Skip(1).ToArray());
}
}
Then you can use it in the view:
@Html.TextBoxFor(model => model.FullName)
Upvotes: 3
Reputation: 2071
You've probably seen the error message you would get with this, in that "Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions."
The idea of EditorFor is that you are taking the existing value and binding the change back into the model. So it makes no sense that you are trying to bind back to two properties. How would it know how to split the text?
If you've created or want to create your own Editor template, you don't have to have a separate property or model. You can use an interface as a basis for an Editor. So if create an interface called, say, INameAndFamily and say that objects based on that interface require Name and Family properties, you can define an Editor that expects that interface.
If you want to be more specific about the editor I can probably help with a more detailed answer.
Upvotes: 0