Renan Sorbito
Renan Sorbito

Reputation: 59

how to return value to a label from controller to view

My code in

Controller:

public ActionResult ShowNewProfessionalWindow(UpdateOrganizationUIDViewModel UOVM)
    {
        Guid OrguID=new Guid(UOVM.Org_UID);
        using (gcserpEntities context = new gcserpEntities())
        {
            var model = new UpdateOrganizationUIDViewModel();
            //model.Org_UID = OrgUID;
            UOVM.org_name_long = context.organizations.Where(c => (c.organization_uid == OrguID)).FirstOrDefault().org_name_long;
            //model.IsAddProfessional = true;
            return View("Index", UOVM);
        }
    }

I want to return a value to a label. what code for my VIEW?

Upvotes: 2

Views: 1529

Answers (5)

Christian Phillips
Christian Phillips

Reputation: 18749

You should use @Html.LabelFor(m => m.org_name_long)

Take a look at this also, for help with Html Helpers

Upvotes: 0

Jatin patil
Jatin patil

Reputation: 4288

You can do it as follows:

Imagine your model is as follows:

public class Model
{
    public string Name { get; set; }
}

Then in your view you can create label as follows:

@Html.LabelFor(model => model.Name)

The above code will render the output as follows:

<label for="Name">Name</label>

Upvotes: 0

LearningNeverEnds
LearningNeverEnds

Reputation: 374

You can try ViewBag.SomeTempFieldName = "your value" or ViewData.SomeTempFieldName = "your value" and then access the value in view page using ViewBag.SomeTempFieldName or ViewData.SomeTempFieldName, in same way how you use model.SomeProperty

use this approach only when you have some specific piece of information needed to be passed from controller to view, don't use it if the value you are looking for is part of model of your view page, in that condition access using Model.Property name (i assume you know how to create models and associate/define for a view page)

Upvotes: 0

Jaimin
Jaimin

Reputation: 8020

try with this,

@model UpdateOrganizationUIDViewModel 

@Html.LabelFor(x => x.MyProperty)

Upvotes: 0

musefan
musefan

Reputation: 48415

Displaying a value in a label can be done like this:

<label>@Model.MyProperty</label>

or, with HTML helper:

@Html.LabelFor(x => x.MyProperty)

If that isn't enough, then you really need to clarify what your problem is.


As @Ingo has says, your need to define the class your are using as your view model. Put this at the top of your view:

@model UpdateOrganizationUIDViewModel

Upvotes: 2

Related Questions