0

I want to fill with a value of the @Html.Editor when the page is loaded. Here is my failed attempt :

 @Html.EditorFor(m => m, new { htmlAttributes = new { @class = "form-control" } })

At the top of the Razor page , I have this :

@model string

And this is my controller :

public ActionResult YeniBelge(string KimlikNo)
{
    return View((object)KimlikNo);
}

It says value can't be null.

How can I make this correct? Thanks.

1

4 Answers 4

1

I suspect you are trying to assign a string that could be null to your model, which can never be null. Create a ViewModel class and use it instead:

public class MyViewModel {
    public string TCKimlikNo { get; set; }
}

public ActionResult YeniBelge(string KimlikNo)
{
    return View(new MyViewModel { KimlikNo = KimlikNo ?? "My Default Value" });
}

And then in your view:

@model MyViewModel

@Html.EditorFor(m => m.TCKimlikNo, new { htmlAttributes = new { @class = "form-control"  } })
Sign up to request clarification or add additional context in comments.

4 Comments

While debugging, I can see the value in YeniBelge controller, so the value is not null. But it still gives that error. Anyway I'm going to try your suggestion. Thanks.
If you have an exception you should have added (message and stacktrace) to the question. Otherwise is not possible to understand what is causing it.
It gives the "Value can't be null exception" in the EditorFor line.
I think @FedericoDipuma has hit the nail on the head, but within the view you are not getting the model. I have tried to show an example below. But Fedrico is the first to answer this.
1

Fill your model with data in controller, then use

@Html.EditorFor(m=>m);

Comments

1

Like @vortex pointed out you need to use the EditorFor template.

@Html.EditorFor(model => model.TCKimlikNo, new { htmlAttributes = new { @class = "form-control"  } })

If you have your model loaded at the top of the page this will fill in the form with the current value of the model field.

Comments

1

In your controller change to the following.

  public class KimlikNoViewmodel
    {
        public string KimlikNo { get; set; }
    }

    public ActionResult YeniBelge (string KimlikNo)
    {
        KimlikNoViewmodel viewModel = new KimlikNoViewmodel();
        viewModel.KimlikNo = KimlikNo;

        return View(viewModel);
    }

in your view I suspect that you are not finding the model. You need to include your project name when calling the Model.

@model YourProjectName.Controllers.KimlikNoViewmodel

I would suggest actually saving the View model we created within the Models folder under a View models file and then you would call the following instead.

@model YourProjectName.Models.KimlikNoViewmodel

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.