문제

I need to get Guid.Empty in controller and pass it to view. I tried to use ViewBag, I added this code in my controller

public class QuestionnaireController : Controller
{
   //....
   ViewBag.EmptyGuid = Guid.Empty;
}

and added this code in view

if (rowobject[6] == ViewBag.EmptyGuid) { //...}

but I got some errors in controller
Error 1 Invalid token '=' in class, struct, or interface member declaration
Error 2 Invalid token ';' in class, struct, or interface member declaration

what's wrong and how to make it works?

UPD
I changed code in my controller (I added ViewBag.EmptyGuid inside method)

[HttpGet]
    public ActionResult QuestionnaireIndex()
    {
        ViewBag.EmptyGuid = Guid.Empty.ToString();
        FillViewBags();
        return View();
    }

and this is script in my view

@section scripts{

<script type="text/javascript"> function buttonize(cellvalue, options, rowobject) {
 var buttons = '';

 if (rowobject[5] == "False") {
     buttons += '<input type="button" value="Edit" onclick="editQuestionnaire(' + options.rowId + ')">';
 }

 buttons += '<input type="button" value="Delete" onclick="deleteQuestionnaire(' + options.rowId + ')">';

 if (rowobject[6] == ViewBag.EmptyGuid) {
     buttons += '<input type="button" value="Publish" onclick="publishQuestionnaire(' + options.rowId + ')">';
 }
 else {
     buttons += '<input type="button" value="Remove" onclick="removePublishQuestionnaire(' + options.rowId + ')">';
 }

 return buttons;
 }
</script>
}
도움이 되었습니까?

해결책

You have code directly in the class, you need a method. For example:

public class QuestionnaireController : Controller
{
    public ActionResult Index()
    {
        ViewBag.EmptyGuid = Guid.Empty;
        return View();
    {
}

This error really has nothing to do with MVC: that is invalid C# syntax and you're getting a compilation error.

On a side note - I'd recommend not using ViewBag at all (or almost ever) when you can use a strongly typed model. Same goes for rowobject[6]: I'm not sure what that is, but you definitely don't want data readers on your View. By the time the data is in the view, it should have already been converted to a model.
On the other hand, I don't think it is wrong to use Guid.Empty or default(Guid) on a view.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top