无效的表达式项 =

Invalid expression term =

伙计们,我似乎无法弄清楚这个。我对 MVC 和 Razor 有点陌生,所以要温柔点:D

我得到了这个局部视图:

@model Phonebook.PresentationLayer.Web.Models.EmailModel
@using Phonebook.BusinessLogicLayer.Managers;

@using (Html.BeginForm("AddEmail", "EmailDetails", FormMethod.Post))
{
    @Model.ParseIds= Model.Id + "/" + Model.Contact.Id;
    @Html.HiddenFor(x => Model.ParseIds)
    <div class="info-table add-email">
        <div>
            @Html.EditorFor(x => x.EmailAddress, new { htmlAttributes = new { @class = "no-borders" } })
            @Html.ValidationMessageFor(x => x.EmailAddress)
        </div>
        <div>
            @{
                EmailTypes emailTypesManager = new EmailTypes();
                IEnumerable<EmailTypeModel> emailTypes = emailTypesManager.GetAll().Select(x => (EmailTypeModel)x);
            }
            @Html.DropDownListFor(x => x.EmailType.Id, new     SelectList(emailTypes, "Id", "Name", Model.EmailType.Id), new { @class = "no-borders-drop" })
            @Html.ValidationMessageFor(x => x.EmailType.Name)
        </div>

        <div>
            <input type="submit" value="Save" class="btn btn-success btn-xs">
        </div>

    </div>
    <button type="button" class="btn btn-block btn-default add-email-button">Add new email</button>
}

我打电话给:

@Html.Partial("Partial/_EmailAdd", new EmailModel(){ Contact = Model.Contact})

但它一直给我这个错误,这对我来说有点不合理:

有人知道问题出在哪里吗?

此外,由于我是 Razor 的新手,我想知道什么时候需要在视图代码中使用分号(“;”)?

谢谢!

我认为这个赋值引起的错误:

@Model.ParseIds= Model.Id + "/" + Model.Contact.Id;

尝试将 Model 赋值包装在 Razor 代码块中,或者更好地在控制器操作方法中赋值:

@* View *@

@{
    Model.ParseIds = Model.Id + "/" + Model.Contact.Id;
}

// Controller

var model = new EmailModel();
// assign Id & Contact.Id here
model.ParseIds = model.Id + "/" + model.Contact.Id;

return PartialView("_EmailAdd", model);

@Model.ParseIds 会将 ParseIds 的值输出为 HTML 中的文本,因此在解析期间跟在 = 之后是无效赋值。

类似问题:

Getting errors with session variable in partialview MVC

好的,所以我将导致错误的行包装在@{}中,如下所示:

@{
 Model.ParseIds= Model.Id + "/" + Model.Contact.Id;
}

并且错误更改为:

Unexpected "{" after "@" character. Once inside the body of a code block (@if {}, @{}, etc.) you do not need to use "@{" to switch to code.

只需删除 @ 和 {} 并使该行保持清晰(因为该行已经在代码块中)就解决了我的问题。

谢谢大家的帮助。