Inputs inside Partialview takes value from main model

前端 未结 1 1473
小鲜肉
小鲜肉 2021-01-27 05:42

I render a PartialView inside my main View

View:

@model FullProductViewModel
...
@Html.Partial(\"_CommentForm\",
相关标签:
1条回答
  • 2021-01-27 05:50

    Your Title property is conflicting with the @{ ViewBag.Title = ".. line of code in your view (the ViewBag code is used by the _Layout.cshtml file to set the global <title> attribute for the page).

    All the HtmlHelper methods that generate form controls set the value by checking (in order)

    1. ModelState
    2. The ViewDataDictionary (ViewData or ViewBag)
    3. The value of the models property

    In your case, nothing has been added to ModelState. But because a value for Title has been added to the ViewDataDictionary, its that value which is used for the value of your textbox. The method never gets to read the actual value you have set in your model.

    The only realistic option (unless you want to modify the code in the _Layout and all views that use it) is to change the name of the property.

    0 讨论(0)
提交回复
热议问题