-1

Guys I can't seem to figure out this one. I am kinda new to MVC and Razor so be gentle :D

I got this partial view:

@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>
}

Which I call with:

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

But it keeps giving me this error which kinda does not make any sense to me: enter image description here

Anyone have an idea where the problem might be?

Also since I'm new to the Razor I was wondering when do I NEED to use semicolon (";") in the View code?

Thank you!

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
Morsus
  • 107
  • 1
  • 16
  • Could you point out which line is #76? The error points to that line so it could narrow the problem down. – Keyur PATEL Oct 17 '17 at 01:48
  • If you are using a viewmodel, then why are you constructing the Model.ParseIds in the view and not when you are populating the view model? That line looks suspect to me. – Fran Oct 17 '17 at 02:00

2 Answers2

1

I think the error caused by this assignment:

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

Try to wrap the Model assignment inside Razor code block, or better to assign it inside controller action method instead:

@* 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 will output value of ParseIds as text in HTML, so that it's an invalid assignment when followed with = during parsing.

Similar issue:

Getting errors with session variable in partialview MVC

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
1

Okay, so I wrapped the line causing the error in @{} like so:

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

And the error changed to:

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

Simply by removing @ and {} leaving the line clear (since the line was already in the block of code) solved my problem.

Thank you all for helping.

Morsus
  • 107
  • 1
  • 16