EditorFor name and name override
I have a simple form to modify part of a complex model. I can, of course, reduce the model, but I would like to solve a difficult case, because eventually we will be dealing with the entire model.
I want the displayed input to have a simple ID and name, but the model name seems impractical.
@Html.EditorFor(model => model.CallInfo.Phone1,
new { @id = "Phone1", @name = "Phone1"})
Should output this
<input type="text" value="" name="Phone1" id="Phone1" class="text-box single-line">
Instead, it outputs this, with the CallInfo prefix being prefixed throughout.
<input type="text" value="" name="CallInfo.Phone1" id="CallInfo_Phone1" class="text-box single-line">
If this cannot be defeated, is there a way to get the CallInfo.Phone1
auto-parse parameter into a parameter for the action method? Because this is not valid syntax in C #.
public ActionResult UpdatePhoneNumber(Int32 profileId, String CallInfo.Phone1)
Again, I understand that I can manually extract it from the request, like
HttpRequest.Current.Params["CallInfo.Phone1"]
But it actually looks like the C # MVC4 conventions should go well here, making ONE of these auto magic widgets. Right.
Update: Following Dmitry's answer, I left the view markup as it was, with the attribute name="CallInfo.Phone1"
as generated and modified by the action signature:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UpdatePhoneNumber(Int32 profileId,
[Bind(Prefix = "CallInfo")] string phone1)
{
// Save it
}
It phone1
is null in the debugger and is not saved to the database. The body of the dispatch, captured from the firearm, has this to say:
Parameters application/x-www-form-urlencoded
CallInfo.Phone1 8121234567
I have also tried using p
in the parameter name phone1
without changing.
source to share
If this cannot be defeated, then is there a way to get CallInfo.Phone1 to parse automatically in a parameter for the action method? Because this is not valid syntax in C #.
Of course, using the attribute [Bind]
:
public ActionResult UpdatePhoneNumber(
int profileId,
[Bind(Prefix = "CallInfo")] string phone1
)
{
...
}
or even better by defining a view model:
public class MyViewModel
{
public int ProfileId { get; set; }
public CallInfoViewModel CallInfo { get; set; }
}
public class CallInfoViewModel
{
public string Phone1 { get; set; }
}
and then, using your controller action, take this view model as an argument:
public ActionResult UpdatePhoneNumber(MyViewModel model)
{
...
}
source to share
If I understand correctly, you want CallInfo.Phone1 and Phone1 to be arguments in Action Method. In this case, your Model should be a container for the CallInfo model and Phone1 (string?) Param.
class bigModel
{
CallInfoType CallInfo { get;set;}
string Phone1 { get;set;}
}
Pass the same model to your view
@model Project.Models.bigModel
and use it in your action signature
public ActionResult Edit(bigModel);
source to share