0

I want to use a (bootstrap) modal to show a translationdialog but it's giving a wrong name. In the browser sourcecode it shows NameTranslations.[0].Translation while it have to be NameTranslations[0].Translation (without the dot between NameTranslation and the [i]).

Some code :

Views/Shared/EditorTemplate/Translation.cshtml

@model List<Data.ViewModels.Shared.TranslationViewModel>

@for (var i = 0; i < Model.Count; i++)
{
    @Html.TextBoxFor(m => Model[i].Translation)
}

Create.cshtml

    @Html.EditorFor(model => Model.NameTranslations,"Translation")

BrowserResult

<input id="NameTranslations__0__Translation" name="NameTranslations.[0].Translation" type="text" value="">

Everything work greats except the name convention is wrong. If I delete the dot in the browsercode, it is well posted in the controller.

3 Answers 3

1

Assign the name attribute by specifying explicit value using new { name = yourdynamicName }

Sign up to request clarification or add additional context in comments.

2 Comments

I tried this @Html.TextBoxFor(m => Model[i].Translation, new { name = Model[i].Translation }) But the result is the same
Then try to assign the value into local variable or frame the local variable value as per your requirement.Like var tempName = Model[i].Translation or tempName=Model[i].Translation+"_"+i and assign the tempName into name attribute.If still not working try to edit the value at runtime and see whether edited value is assigned or not
0

Make the changes to the Translation template to be an editor template for a single entity only:

@model Data.ViewModels.Shared.TranslationViewModel

@Html.TextBoxFor(m => m.Translation)
...

and iterate through the collection in the main view instead:

@for (int i = 0; i < Model.NameTranslations.Count; i++)
{
    @Html.EditorFor(m => m.NameTranslations[i], "Translation")
}

2 Comments

This won't work great because I want to make a dialog with a table in it.. But it's a possible solution if i find nothing else :)
If you need this in dialog, use a partial view instead of a view.
0

I fixed this with a ViewData

Create.cshtml

@model List<Data.ViewModels.Shared.TranslationViewModel>
@{
    var ModelName = ViewData["ModelName"];
}

Partial View: Views/Shared/_Translation.cshtml

@for (var i = 0; i < Model.Count; i++)
{
    var LanguageID = ModelName + "[" + i + "].LanguageID";
    var TranslationName= ModelName + "[" + i + "].Translation";

    <input type="hidden" name="@LanguageID" value="@Model[i].LanguageID" />
    <input type="text" name="@TranslationName" value="@Model[i].Translation" />
    <br />
}

I think it isn't the best way to do it but if there's a change in a ViewModel, you can replace it in one place (partial) instead of several.

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.