Chris
Chris

Reputation: 81

MVC4 cshtml page function call

I need to display a value in an editable textbox on my mvc page when it first loads if it exists. I've got a function that will take care of getting the value that I need, but I need to pass in parameters from the current model to get what I need from the database. The problem I'm having is getting this value into the textbox. What I tried was

cshtml:

@Html.TextBoxFor(model => model.AdjustedLiabilityAmount, new { @Value=OBS_LIB.BLL.JeopardyAssessment.JeopardyAssessment.GetLatestAdjustedLiabilityAmount(Model.DOTNumber, Model.LiabilityAmount))}

I get a red squiggly that "The name 'Value' does not exist in the current context"

So I tried a different technique I read about which was like this.

Controller:

public ActionResult Index()
    {
        ViewBag.AdjustedValue = OBS_LIB.BLL.JeopardyAssessment.JeopardyAssessment.GetLatestAdjustedLiabilityAmount(Model.DOTNumber, Model.LiabilityAmount);

cshtml:

@Html.TextBoxFor(model => model.AdjustedLiabilityAmount, new { @Value=ViewBag.AdjustedValue)}

This time I'm getting the red squiggly "The name 'Model' does not exist in the current context."

I'm sure I'm just missing something basic here as I'm new to MVC.

Any help is much appreciated.

Entire ActionResult Index:

public ActionResult Index()
    {
        ViewBag.AdjustedValue = OBS_LIB.BLL.JeopardyAssessment.JeopardyAssessment.GetLatestAdjustedLiabilityAmount(Model.DOTNumber, Model.LiabilityAmount);
        var Report = new OBS_LIB.DTO.JeopardyAssessmentReport();
        Report.Stage = 1;
        Report.Status = "Active";
        Report.ReportItems = OBS_LIB.BLL.JeopardyAssessment.JeopardyAssessment.GetJAReportItems(Report.Stage, Report.Status);
        return View(Report);
    }

Upvotes: 1

Views: 837

Answers (3)

You want to do something like this:

Class:

public class ModelClassHere {
     public float Liability {get;set;}
}

Controller:

public ActionResult Index(ModelClassHere model) {
    model.Liability = 10.00;
    return View(model); // pass model to the view
}

View:

@Html.TextBoxFor(x => x.Liability) // 'x' can be anything

EDIT*

If you already have a model and need to pass one simple value:

Controller:

public ActionResult Index(ModelClassHere model, string otherValue) {
    model.Liability = 10.00;
    ViewBag.Liability = model.Liability;
    return View(model); // pass model to the view
}

View:

<input type="text" id="otherValue" name="otherValue" value="@ViewBag.Liability.ToString()" />

Upvotes: 2

mejiajuanbta
mejiajuanbta

Reputation: 176

You can use @Html.TextBox("AdjustedLiabilityAmount", (Decimal)ViewBag.AdjustedValue)}

Or

@Html.TextBox("AdjustedLiabilityAmount", Model.AdjustedLiabilityAmount == null ? (Decimal)ViewBag.AdjustedValue : Model.AdjustedLiabilityAmount)}

In decimal type you put your the type that you need.

Upvotes: 2

Danimal
Danimal

Reputation: 333

You need to pass your model in the Controller

    return view(myModelName);

make sure you have access to it in your controller.

also your view has to reference the model in the @model line at the top.

Finally to call the model it would be

view:

    Model.myModelName

Upvotes: 1

Related Questions