3

I have this controller :

public ActionResult MyController(string myString)
{
    return View((object)myString);
}

I'm trying to pass the string to view like this :

@model string
@Html.EditorFor(m => m)

I get value can't be null error. How can I fix this? Thanks.

1
  • Refer this question/answer for how to handle this. Commented Apr 9, 2017 at 22:23

2 Answers 2

7

First of all, I would recommend to change your Action method name. Why you name it like MyController. It should be a meaningful method name.

Then come to your issue. If your view is only for display purpose, not for form post, then you can bind your string in a viewbag and render in your view.

For example, in your action method,

ViewBag.MyString = myString;

And in your view,

<p>@ViewBag.MyString</p>

But if you want to edit your string in view and after click a submit button, it should post the value to server, then create a view model, for example,

public class MyStringModel
{
  public string MyString { get; set; }
}

In your action method,

public ActionResult MyController(string myString)
{
  MyStringModel = new MyStringModel();
  MyStringModel.MyString = myString;
  return View(MyStringModel)
}

Then in your view,

@model MyStringModel
@Html.EditorFor(m => m.MyString)

See, you need to add @HTML.BeginForm and a submit button in your view to post back your string data.

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

3

Also you can pass information to your view with the ViewBag, ViewData, TempData all with different information lifecyle.

Check this:

http://royalarun.blogspot.com.ar/2013/08/viewbag-viewdata-tempdata-and-view.html

With your example the model is associated with a dictionary so you can not use directly the property like this.

For your example and just pass only a string from the controller you can do any like this:

public ActionResult MyController(string myString)
{
    return View(model:myString);  
}

and in the .cshtml (if you use c#)

@model string
@{
    var text = Model;
}
@Html.EditorFor(m => text);

But I think is a better solution pass a viewMoedel with a string property like the @Stephen Muecke response Prefill Html Editor Asp.net MVC

8 Comments

Wrong - it does need to be cast to object (other wise if would call the overload where the parameter is the view name)
Yes you are right, but in the example I use model param declaration, look again :) @ Stephen Muecke
That's fine, but your first statement is wrong and misleading (OP's usage was fine)!
Maybe I can change to it's not really necesary but it's fine to OP ? :)
The issue is with the HtmlHelper method and the solution is to use a view model as per the link in my comment to the question :)
|

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.