7

im getting error in view- ""test1.cshtml"

Unexpected "if" keyword after "@" character. Once inside code, you do not need to prefix constructs like "if" with

test1.cshtml

    @model WebApplication9.Models.Names


@using (Html.BeginForm())
{
    @Html.TextBoxFor(m => m.MyName)

    <button type="submit">Submit</button>

    if (!string.IsNullOrWhiteSpace(Model.MyName))
    {
        <p>welcome, your name is @Model.MyName</p>
    }
}

controller code

 public ActionResult test1()
        {

            Names name = new Names();

            return View(name);

            ViewBag.Message = "Your contact page.";

            return View();
        }
        [HttpPost]
        public ActionResult test1(string name)
        {
            ViewBag.Message = "Hello what is ur name ???";
            ViewBag.Name = name;
            return View();
        }

model code

namespace WebApplication9.Models
{
    public class Names
    {
        public string MyName { get; set; }
    }
}
STACK2
  • 165
  • 1
  • 5
  • 16

3 Answers3

14

Try:

@model WebApplication9.Models.Names


@using (Html.BeginForm())
{
    @Html.TextBoxFor(m => m.Name)

    <button type="submit">Submit</button>

    if (!string.IsNullOrWhiteSpace(Model.MyName))
    {
        <p>welcome, your name is @Model.MyName</p>
    }
}

Since you have the @using, the Razor code inside it does not need the @. But, if you have an HTML element, then you will need to use the @, like on the Welcome text.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Denis V
  • 421
  • 4
  • 17
  • that error solved but i got one more error , if (!string.IsNullOrWhiteSpace(Model.MyName)) Object reference not set to an instance of an object. – STACK2 Apr 29 '16 at 13:39
  • @STACK2 You have 2 properties defined, `Name` and `MyName`. One of the two don't exist. Decide on which one you want to use and stick with it. – Brendan Vogt Apr 29 '16 at 13:41
  • If it solved your primary problem that is described on question title, accept the answer plz. – Denis V Apr 29 '16 at 13:49
  • "Try" is not an answer. Explain what you changed. – CodeCaster Apr 29 '16 at 14:11
1

There's no need to add @ sign in front of "if". The code should be like this:

@model WebApplication9.Models.Names
@using (Html.BeginForm())
{
      Html.TextBoxFor(m => m.Name)
      <button type="submit">Submit</button>
     if (!string.IsNullOrWhiteSpace(Model.MyName))
     {
        <p>welcome, your name is @Model.MyName</p>
     }
}
Uğur Demirel
  • 71
  • 1
  • 2
  • 9
Auguste
  • 2,007
  • 2
  • 17
  • 25
0

You have to remove the @ symbol in front of your if statement.

This is how it should look:

@using (Html.BeginForm())
{
    @Html.TextBoxFor(m => m.MyName)

    <button type="submit">Submit</button>

    if (!string.IsNullOrWhiteSpace(Model.MyName))
    {
        <p>welcome, your name is @Model.MyName</p>
    }
}

You code is not consistent here, you are using 2 different property names, namely Name and MyName. Use only one.

You are also not populating the MyName property any where? You are using IsNullOrWhiteSpace on a property that doesn't have a set value. Try this as well:

public ActionResult test1()
{
     Names name = new Names();
     name.MyName = "Clown";

     return View(name);
}

You Index action method has 2 return views, remove one.

Please spend time working through some ASP.NET MVC tutorials. It will take some time learning but in the long run it is for the best.

Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234