0

I'm not sure how to search for this as I'm not sure what it would be called

I have a model like so

class MyModel
{
  public Firstbit {get; set; }
  public Secondbit {get; set; }
}

In my View I have something like this

<div>
  @Html.TextBoxFor(x => x.Firstbit.A)
  @Html.TextBoxFor(x => x.Firstbit.B)
  @Html.TextBoxFor(x => x.Firstbit.C)
</div>
<div>
  @Html.TextBoxFor(x => x.Secondbit.A)
  @Html.TextBoxFor(x => x.Secondbit.B)
  @Html.TextBoxFor(x => x.Secondbit.C)
</div>

Other than by using partials or splitting the file up can I have a local variable to save writing the FirstBit/secondbit every time? i.e.

<div>
  @{ var a = @Model.Firstbit; }
  // somehow address 'a' below?
  @Html.TextBoxFor(x => x.A)
  @Html.TextBoxFor(x => x.B)
  @Html.TextBoxFor(x => x.C)
</div>

thanks

2 Answers 2

4

Probably not. Nor should you even want to do this.

For starters, any time you have to declare a variable in your view you're probably doing something wrong. Views should really just bind to their backing models, at most with some conditional or looping logic.

Take a step back and ask... What would this really achieve? You'd save a few keystrokes, but you'd make the code less standard and more difficult to follow. Keystrokes aren't the difficult part. Typing code into an IDE isn't the difficult part. Supporting that code later (either being supported by somebody else or even by yourself some time after you wrote it and forgot about it) is the difficult part.

Keep the code simple, easy to read, easy to understand, and clear and expressive about what it's doing. This code tells you exactly what it's doing:

@Html.TextBoxFor(x => x.Secondbit.C)

It's creating a text box for the C property of the Secondbit property of the model.

Now, if the model is starting to have too many properties and the view is starting to become too large, one thing you can (and depending on the situation perhaps should) do is break apart your view into multiple partial views, each of which addresses a particular model in your large composite model.

For example, you might create a partial view which specifically builds markup for viewing and/or editing a model of type Firstbit. Then in your parent view you'd delegate that model instance to the partial view:

@Html.Partial("FirstbitView", Model.Firstbit)

After all, since the model is a composite of smaller models, it makes sense for the view to be a composite of smaller views. This organizes the code into discrete components rather than into some kind of shorthand notation on a single component.

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

1 Comment

K, thanks, that is what i suspected. I'm not fond of the code duplication but past a certain point I'd use a partial anyway
0

You could create a partial view or a display template for that. To avoid code duplication you could define an interface an use it as the model.

Here's is a sample using a partial view. First we define and implement an Ibit interface

public class MyModel
{
    public Firstbit Firstbit { get; set; }
    public Secondbit Secondbit { get; set; }
}

public interface Ibit
{
    string A { get; set; }
    string B { get; set; }
    string C { get; set; }
}
public class Firstbit : Ibit    
{
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
}

public class Secondbit : Ibit    
{
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
}

Then we use this interface as the model inside the partial view (ex IBitPartial.cshtml):

@model WebApplicationMVC.Model.Ibit

    <div>
        @Html.TextBoxFor(model => model.A)

        @Html.TextBoxFor(model => model.B)

        @Html.TextBoxFor(model => model.C)

    </div>

And we can use it from the main view :

@model WebApplicationMVC.Model.MyModel

<div>
   @Html.Partial("IBitPartial", Model.Firstbit)

   @Html.Partial("IBitPartial", Model.Secondbit)
</div>
<h2>IBitMain</h2>

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.